0

我正在使用stat_density2d()ggplot2 的功能在城市地图下绘制密度核图。问题是城市周围有海洋,我希望图形的颜色尊重多边形的限制,即不要逆着海洋前进。可能,为了实现这一点,有必要改变“影子”的大小。对不起数据的大小,但这只是我原始地图的一个较小的多边形。但基本上是经纬度和ggplot2代码之后。

long=c(-38.4012,-38.4013,-38.4013,-38.4013,-38.4014,-38.4014,-38.4014,-38.4014,-38.4015,-38.4015,-38.4015,-38.4016,-38.4016,-38.4016,-38.4017,-38.4017,-38.4017,-38.4017,-38.4018,-38.4018,-38.4018,-38.4019,-38.4019,-38.4019,-38.4019,-38.402,-38.402,-38.402,-38.4021,-38.4021,-38.4021,-38.4021,-38.4022,-38.4022,-38.4022,-38.4022,-38.4023,-38.4023,-38.4023,-38.4024,-38.4024,-38.4024,-38.4024,-38.4025,-38.4025,-38.4025,-38.4025,-38.4026,-38.4026,-38.4026,-38.4026,-38.4027,-38.4027,-38.4027,-38.4028,-38.4028,-38.4028,-38.4028,-38.4029,-38.4029,-38.4029,-38.4029,-38.403,-38.403,-38.403,-38.403,-38.4031,-38.4031,-38.4031,-38.4032,-38.4032,-38.4032,-38.4032,-38.4033,-38.4033,-38.4033,-38.4033,-38.4034,-38.4034,-38.4034,-38.4034,-38.4035,-38.4035,-38.4035,-38.4036,-38.4036,-38.4036,-38.4036,-38.4037,-38.4037,-38.4037,-38.4038,-38.4038,-38.4038,-38.4039,-38.4039,-38.4039,-38.4039,-38.404,-38.404,-38.404,-38.4041,-38.4041,-38.4041,-38.4042,-38.4042,-38.4042,-38.4043,-38.4043,-38.4043,-38.4044,-38.4044,-38.4044,-38.4045,-38.4045,-38.4045,-38.4045,-38.4046,-38.4046,-38.4047,-38.4047,-38.4047,-38.4047,-38.4048,-38.4048,-38.4048,-38.4049,-38.4049,-38.4049,-38.405,-38.405,-38.405,-38.4051,-38.4051,-38.4051,-38.4051,-38.4052,-38.4052,-38.4052,-38.4053,-38.4053,-38.4053,-38.4054,-38.4054,-38.4054,-38.4054,-38.4055,-38.4055,-38.4055,-38.4056,-38.4056,-38.4056,-38.4056,-38.4057,-38.4057,-38.4057,-38.4057,-38.4058,-38.4058,-38.4058,-38.4059,-38.4059,-38.4059,-38.406,-38.406,-38.406,-38.406,-38.4061,-38.4061,-38.4061,-38.4062,-38.4062,-38.4062,-38.4062,-38.4063,-38.4063,-38.4063,
       -38.4064,-38.4064,-38.4064,-38.4064,-38.4065,-38.4065,-38.4065,-38.4066,-38.4066,-38.4066,-38.4066,-38.4067,-38.4067,-38.4067,-38.4068,-38.4068,-38.4068,-38.4069,-38.4069,-38.4069,-38.4069,-38.407,-38.407,-38.407,-38.4071,-38.4071,-38.4071,-38.4071,-38.4072,-38.4072,-38.4072,-38.4073,-38.4073,-38.4073,-38.4074,-38.4074,-38.4074,-38.4075,-38.4075,-38.4075,-38.4076,-38.4076,-38.4076,-38.4076,-38.4077,-38.4077,-38.4077,-38.4078,-38.4078,-38.4078,-38.4079,-38.4079,-38.4079,-38.4079,-38.408,-38.408,-38.408,-38.4081,-38.4081,-38.4082,-38.4082,-38.4082,-38.4083,-38.4083,-38.4083,-38.4083,-38.4084,-38.4084,-38.4084,-38.4085,-38.4085,-38.4086,-38.4086,-38.4086,-38.4087,-38.4087,-38.4087,-38.4087,-38.4088,-38.4088,-38.4088,-38.4089,-38.4089,-38.4089,-38.4089,-38.409,-38.409,-38.409,-38.409,-38.4091,-38.4091,-38.4091,-38.4092,-38.4092,-38.4092,-38.4093,-38.4093,-38.4093,-38.4093,-38.4094,-38.4094,-38.4094,-38.4094,-38.4095,-38.4095,-38.4095,-38.4095,-38.4096,-38.4096,-38.4096,-38.4097,-38.4097,-38.4097,-38.4097,-38.4098,-38.4098,-38.4098,-38.4099,-38.4099,-38.4099,-38.41,-38.41,-38.41,-38.41,-38.4101,-38.4101,-38.4101,-38.4102,-38.4102,-38.4102,-38.4103,-38.4103,-38.4103,-38.4103,-38.4104,-38.4104,-38.4104,-38.4105,-38.4105,-38.4105,-38.4105,-38.4106,-38.4106,-38.4106,-38.4107,-38.4107,-38.4107,-38.4108,-38.4108,-38.4108,-38.4109,-38.4109,-38.4109,-38.411,-38.411,-38.411,-38.411,-38.4111,-38.4111,-38.4111,-38.4112,-38.4112,-38.4112,-38.4112,-38.4113,-38.4113,-38.4113,-38.4114,-38.4114,-38.4114,-38.4115,-38.4115,-38.4115,-38.4115,-38.4116,-38.4116,-38.4116,-38.4117,-38.4117,-38.4117,-38.4118,-38.4118,-38.4118,-38.4119,-38.4119,-38.4119,-38.412,-38.412,-38.412,-38.412,-38.4121,-38.4121,-38.4121,-38.4122,-38.4122,-38.4122,-38.4123,-38.4123,-38.4123,-38.4124,-38.4124,-38.4125,-38.4125,-38.4126,-38.4127,-38.4128,-38.4128,-38.4129,-38.413,-38.4131,-38.4131,-38.4132,-38.4133,-38.4134,-38.4135,-38.4135,-38.4136,-38.4136,-38.4137,-38.4138,-38.4138,-38.4139,-38.4139,-38.414,-38.4141,-38.4142,-38.4143,-38.4143,-38.4144,-38.4145,-38.4146,-38.4146,-38.4147,-38.4148,-38.4149,-38.4149,-38.415,-38.4151,-38.4152,-38.4153,-38.4153,-38.4154,-38.4155,-38.4156,-38.4156,-38.4157,-38.4158,-38.4159,-38.4159,-38.416,-38.4161,-38.4162,-38.4163,-38.4163,-38.4164,-38.4165,-38.4166,-38.4167,-38.4167,-38.4168,-38.4169,-38.417,-38.417,-38.417,-38.4171,-38.4172,-38.4172,-38.4173,-38.4174,-38.4174,-38.4179,-38.418,-38.418,-38.4183,-38.4189,-38.419,-38.419,-38.4192,-38.4194,-38.4197,-38.4197,-38.4199,-38.42,-38.4203,-38.4206,-38.4217,-38.4221,-38.4223,-38.4226,-38.4229,-38.4232,-38.4236,-38.4236,-38.4234,-38.4231,-38.4231,-38.423,-38.423,-38.423,-38.423,-38.4231,-38.4234,-38.4237,-38.4241,-38.4248,-38.425,-38.4255,-38.4257,-38.426,-38.4263,-38.4263,-38.4264,-38.4264,-38.4266,-38.4267,-38.4269,-38.4271,-38.4273,-38.4274,-38.4276,-38.4263,-38.4265,-38.4265,-38.4265,-38.4265,-38.4265,-38.4264,-38.4264,-38.4264,-38.4265,-38.4265,-38.4266,-38.4266,-38.4267,-38.4268,-38.4269,-38.4271,-38.4272,-38.4274,-38.4274,-38.4275,-38.4277,-38.4277,-38.4278,-38.4279,-38.428,-38.4281,-38.4282,-38.4283,-38.4283,-38.4284,-38.4285,-38.4285,-38.4286,-38.4286,-38.4287,-38.4288,-38.4288,-38.4289,-38.4289,-38.4289,-38.4289,-38.4289,-38.4289,-38.4296,-38.4295,-38.4289,-38.4284,-38.4278,-38.4272,-38.4267,-38.4261,-38.4257,-38.4252,-38.4245,-38.4241,-38.4236,-38.4231,-38.4226,-38.4221,-38.4216,-38.421,-38.4207,-38.4204,-38.4204,-38.4198,-38.4193,-38.419,-38.4186,-38.4178,-38.4169,-38.4167,-38.4165,-38.4154,-38.4153,-38.4143,-38.4138,-38.4133,-38.4131,-38.4126,-38.4124,-38.4118,-38.4111,-38.4104,-38.4097,-38.409,-38.4084,-38.4077,-38.4076,-38.4076,-38.4075,-38.4073,-38.4073,-38.4073,-38.4072,-38.4073,-38.4073,-38.4073,-38.4075,-38.4077,-38.4078,-38.408,-38.408,-38.408,-38.408,-38.408,-38.4079,-38.4078,-38.4075,-38.4074,-38.4073,-38.4072,-38.4069,-38.4068,-38.4062,-38.4058,-38.4049,-38.4046,-38.4044,-38.4037,-38.4036,-38.4035,-38.4031,-38.4028,-38.4023,-38.4019,-38.4012)

lat=c(-12.9646,-12.9646,-12.9646,-12.9646,-12.9646,-12.9647,-12.9647,-12.9647,-12.9647,-12.9647,-12.9648,-12.9648,-12.9648,-12.9648,-12.9648,-12.9649,-12.9649,-12.9649,-12.9649,-12.965,-12.965,-12.965,-12.965,-12.965,-12.9651,-12.9651,-12.9651,-12.9651,-12.9651,-12.9652,-12.9652,-12.9652,-12.9652,-12.9653,-12.9653,-12.9653,-12.9653,-12.9654,-12.9654,-12.9654,-12.9654,-12.9655,-12.9655,-12.9655,-12.9655,-12.9656,-12.9656,-12.9656,-12.9657,-12.9657,-12.9657,-12.9657,-12.9657,-12.9658,-12.9658,-12.9658,-12.9658,-12.9659,-12.9659,-12.9659,-12.9659,-12.966,-12.966,-12.966,-12.966,-12.9661,-12.9661,-12.9661,-12.9661,-12.9662,-12.9662,-12.9662,-12.9662,-12.9663,-12.9663,-12.9663,-12.9663,-12.9664,-12.9664,-12.9664,-12.9664,-12.9665,-12.9665,-12.9665,-12.9665,-12.9666,-12.9666,-12.9666,-12.9666,-12.9666,-12.9667,-12.9667,-12.9667,-12.9667,-12.9668,-12.9668,-12.9668,-12.9668,-12.9668,-12.9669,-12.9669,-12.9669,-12.9669,-12.9669,-12.9669,-12.967,-12.967,-12.967,-12.967,-12.967,-12.967,-12.967,-12.9671,-12.9671,-12.9671,-12.9671,-12.9671,-12.9671,-12.9672,-12.9672,-12.9672,-12.9672,-12.9672,-12.9672,-12.9673,-12.9673,-12.9673,-12.9673,-12.9673,-12.9674,-12.9674,-12.9674,-12.9674,-12.9674,-12.9675,-12.9675,-12.9675,-12.9675,-12.9676,-12.9676,-12.9676,-12.9676,-12.9676,-12.9677,-12.9677,-12.9677,-12.9677,-12.9677,-12.9678,-12.9678,-12.9678,-12.9678,-12.9679,-12.9679,-12.9679,-12.9679,-12.968,-12.968,-12.968,-12.968,-12.968,-12.9681,-12.9681,-12.9681,-12.9681,-12.9682,-12.9682,-12.9682,-12.9682,-12.9683,-12.9683,
      -12.9683,-12.9683,-12.9683,-12.9684,-12.9684,-12.9684,-12.9684,-12.9685,-12.9685,-12.9685,-12.9685,-12.9685,-12.9686,-12.9686,-12.9686,-12.9686,-12.9687,-12.9687,-12.9687,-12.9687,-12.9687,-12.9688,-12.9688,-12.9688,-12.9688,-12.9689,-12.9689,-12.9689,-12.9689,-12.969,-12.969,-12.969,-12.969,-12.969,-12.969,-12.9691,-12.9691,-12.9691,-12.9691,-12.9691,-12.9691,-12.9692,-12.9692,-12.9692,-12.9692,-12.9693,-12.9693,-12.9693,-12.9693,-12.9693,-12.9694,-12.9694,-12.9694,-12.9694,-12.9694,-12.9695,-12.9695,-12.9695,-12.9695,-12.9695,-12.9696,-12.9696,-12.9696,-12.9696,-12.9696,-12.9696,-12.9696,-12.9697,-12.9697,-12.9697,-12.9697,-12.9697,-12.9697,-12.9698,-12.9698,-12.9698,-12.9698,-12.9698,-12.9698,-12.9698,-12.9699,-12.9699,-12.9699,-12.9699,-12.9699,-12.97,-12.97,-12.97,-12.97,-12.9701,-12.9701,-12.9701,-12.9701,-12.9702,-12.9702,-12.9702,-12.9702,-12.9703,-12.9703,-12.9703,-12.9703,-12.9703,-12.9704,-12.9704,-12.9704,-12.9704,-12.9705,-12.9705,-12.9705,-12.9705,-12.9706,-12.9706,-12.9706,-12.9706,-12.9707,-12.9707,-12.9707,-12.9707,-12.9708,-12.9708,-12.9708,-12.9708,-12.9709,-12.9709,-12.9709,-12.9709,-12.9709,-12.9709,-12.971,-12.971,-12.971,-12.971,-12.9711,-12.9711,-12.9711,-12.9711,-12.9711,-12.9712,-12.9712,-12.9712,-12.9712,-12.9712,-12.9713,-12.9713,-12.9713,-12.9713,-12.9713,-12.9714,-12.9714,-12.9714,-12.9714,-12.9714,-12.9714,-12.9715,-12.9715,-12.9715,-12.9715,-12.9715,-12.9716,-12.9716,-12.9716,-12.9716,-12.9717,-12.9717,-12.9717,-12.9717,-12.9717,-12.9718,-12.9718,-12.9718,-12.9718,-12.9718,-12.9718,-12.9718,-12.9719,-12.9719,-12.9719,-12.9719,-12.9719,-12.972,-12.972,-12.972,-12.972,-12.972,-12.9721,-12.9721,-12.9721,-12.9721,-12.9721,-12.9721,-12.9722,-12.9722,-12.9722,-12.9722,-12.9723,-12.9723,-12.9723,-12.9723,-12.9724,-12.9724,-12.9724,-12.9725,-12.9725,-12.9725,-12.9725,-12.9725,-12.9726,-12.9726,-12.9726,-12.9727,-12.9728,-12.9728,-12.9729,-12.9729,-12.9729,-12.973,-12.9731,-12.9731,-12.9732,-12.9732,-12.9732,-12.9733,-12.9733,-12.9733,-12.9734,-12.9734,-12.9735,-12.9735,-12.9735,-12.9736,-12.9736,-12.9737,-12.9737,-12.9738,-12.9738,-12.9739,-12.9739,-12.974,-12.974,-12.974,-12.9741,-12.9742,-12.9742,-12.9743,-12.9743,-12.9743,-12.9744,-12.9745,-12.9745,-12.9746,-12.9746,-12.9747,-12.9747,-12.9748,-12.9748,-12.9748,-12.9749,-12.9749,-12.975,-12.975,-12.9751,-12.9751,-12.9752,-12.9752,-12.9753,-12.9753,-12.9753,-12.9754,-12.9754,-12.9755,-12.9755,-12.9756,-12.9756,-12.9757,-12.9758,-12.9758,-12.9751,-12.9751,-12.9751,-12.9752,-12.9753,-12.9754,-12.9752,-12.9748,-12.9739,-12.973,-12.9729,-12.9725,-12.9722,-12.9719,-12.9716,-12.9705,-12.97,-12.9698,-12.9696,-12.9694,-12.9693,-12.9692,-12.9691,-12.9686,-12.9675,-12.9674,-12.9673,-12.9666,-12.9658,-12.9654,-12.9652,-12.964,-12.9638,-12.9636,-12.9636,-12.9635,-12.9632,-12.963,-12.9627,-12.9625,-12.9623,-12.962,-12.9617,-12.9616,-12.9616,-12.9615,-12.9615,-12.9615,-12.9615,-12.9616,-12.9589,-12.9587,-12.9587,-12.9585,-12.9584,-12.9584,-12.9584,-12.9583,-12.9583,-12.9581,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.958,-12.9581,-12.9581,-12.958,-12.958,-12.958,-12.958,-12.958,-12.9579,-12.9579,-12.9578,-12.9578,-12.9577,-12.9577,-12.9576,-12.9576,-12.9575,-12.9575,-12.9574,-12.9573,-12.9572,-12.9572,-12.9571,-12.957,-12.9566,-12.9565,-12.9556,-12.955,-12.9542,-12.9534,-12.9527,-12.9518,-12.9514,-12.9508,-12.9498,-12.9493,-12.9485,-12.9479,-12.9472,-12.9465,-12.9458,-12.945,-12.9446,-12.9443,-12.9442,-12.9434,-12.9428,-12.9423,-12.9427,-12.9432,-12.9439,-12.944,-12.9442,-12.9449,-12.945,-12.9457,-12.9461,-12.9465,-12.9467,-12.9471,-12.9474,-12.948,-12.9488,-12.9495,-12.9504,-12.9511,-12.9518,-12.9525,-12.9526,-12.9527,-12.9529,-12.9533,-12.9534,-12.9536,-12.9538,-12.954,-12.9542,-12.9546,-12.9555,-12.9563,-12.9571,-12.958,-12.9581,-12.9584,-12.9586,-12.9588,-12.9591,-12.9594,-12.9598,-12.9599,-12.96,-12.9601,-12.9603,-12.9604,-12.9608,-12.9611,-12.9618,-12.9619,-12.962,-12.9625,-12.9626,-12.9627,-12.963,-12.9631,-12.9636,-12.964,-12.9646)

group<- rep(0.1,length(long))

p3<-data.frame(long=c(-38.41799,-38.41193,-38.41927,-38.42362,-38.41234,-38.41330,-38.40912),
               lat =c(-12.96622,-12.96708,-12.96775,-12.95499,-12.95798,-12.95816,-12.95501))


ggplot() + geom_polygon(aes(x = long, y = lat, group=group), colour = "black",fill="white")+
  geom_polygon() + theme_bw()+  theme(panel.background = element_rect(fill = "light blue"))+
  stat_density2d(aes(x=long, y=lat, fill = ..level..), alpha=0.25,data=as.data.frame(p3), geom="polygon")+
  scale_fill_gradientn(colours=rev(brewer.pal(11,"Spectral")), name="Kernel density",breaks=c(0,200),
                       labels=c("Min","Max"),limits=c(0,200))  

输出是:

内核映射

4

0 回答 0