查看原文
其他

R语言可视化——地图填充与散点图图层叠加

2016-10-13 小魔方 数据小魔方

今天跟大家分享关于如何在地图图层上添加散点图。


散点图需要精确的经纬度信息才能在叠加的图层上进行映射,因此我们选用中国省级轮廓地图以及各省省会城市的经纬度进行案例演示。



加载包:


library(ggplot2)

library(plyr)

library(maptools)

library(sp)



导入中国省界地图:

china_map<-readShapePoly("c:/rstudy/bou2_4p.shp")

data1<- china_map@data      

data2<- data.frame(id=row.names(data1),data1) 


数据格式转化及业务数据合并:

china_map1 <- fortify(china_map) 

china_map_data <- join(china_map1,data2, type = "full") 

mydata <- read.csv("c:/rstudy/geshengzhibiao.csv")

china_data <- join(china_map_data, mydata, type="full")



各省省会城市经纬度数据:

province_city <- read.csv("c:/rstudy/chinaprovincecity.csv") 



省级轮廓地图上添加散点图图层:


ggplot(china_data,aes(long,lat))+

     geom_polygon(aes(group=group),fill="white",colour="grey60")+

     geom_point(data=province_city,aes(x=jd,y=wd),colour="red")+

     coord_map("polyconic") + 

     theme(

          panel.grid = element_blank(),

          panel.background = element_blank(),

          axis.text = element_blank(),

          axis.ticks = element_blank(),

          axis.title = element_blank()

          )




接下来,我们可以给各省的省会城市赋值,将散点图的大小映射给连续性数值变量,使其变成气泡图。


province_city$zhibiao<-NULL

province_city$zhibiao<-rnorm(34,100,50)


windowsFonts(myFont = windowsFont("微软雅黑"))



ggplot()+

     geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="grey95",colour="grey80")+

     geom_point(data=province_city,aes(x=jd,y=wd,size=zhibiao),shape=21,fill="#8E0F2E",colour="black",alpha=0.4)+

     scale_size_area(max_size=8)+

     coord_map("polyconic") + 

     guides(size=guide_legend(reverse=TRUE,title=NULL))+ 

     ggtitle("某公司2015~2016年度营业状况分布图")+

     theme(

          title=element_text(family="myFont"),

          panel.grid = element_blank(),

          panel.background = element_blank(),

          axis.text = element_blank(),

          axis.ticks = element_blank(),

          axis.title = element_blank(),

          legend.position =c(0.15,0.4),

          legend.background=element_rect(colour="white",fill="white"),

          legend.text.align=1

          )



最后我们来处理标签问题:


因为这里使用了散点图(气泡图)作为数据展示方式,而作为底图的地图图层仅仅是作为定位信息,没有包含任何的数据信息,而且页面整体上没有太多地方放置省名称标签,所以我们有选择性的显示前五个数据所代表的省份标签,以防标签太多导致页面杂乱。



labelper<-province_city[order(province_city[,5],decreasing=T),][1:10,]


ggplot()+

     geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="grey95",colour="grey80")+

     geom_point(data=province_city,aes(x=jd,y=wd,size=zhibiao),shape=21,fill="#8E0F2E",colour="black",alpha=0.4)+

     scale_size_area(max_size=8)+

     coord_map("polyconic") + 

     geom_text(aes(x=jd+2.3,y=wd,label=city),size =3,family="myFont",fontface="plain",data=labelper) +

     guides(size=guide_legend(reverse=TRUE,title=NULL))+ 

     ggtitle("某公司2015~2016年度营业状况分布图")+

     theme(

          title=element_text(family="myFont"),

          panel.grid = element_blank(),

          panel.background = element_blank(),

          axis.text = element_blank(),

          axis.ticks = element_blank(),

          axis.title = element_blank(),

          legend.position =c(0.15,0.4),

          legend.background=element_rect(colour="white",fill="white"),

          legend.text.align=1

          )




如果甲你想要添加全部的标签,直接使用province_city中的city标签即可:


ggplot()+

     geom_polygon(data=china_data,aes(x=long,y=lat,group=group),fill="grey95",colour="grey80")+

     geom_point(data=province_city,aes(x=jd,y=wd,size=zhibiao),shape=21,fill="#8E0F2E",colour="black",alpha=0.4)+

     scale_size_area(max_size=8)+

     coord_map("polyconic") + 

     geom_text(aes(x=jd+2.3,y=wd,label=city),size =3,family="myFont",fontface="plain",data=province_city) +

     guides(size=guide_legend(reverse=TRUE,title=NULL))+ 

     ggtitle("某公司2015~2016年度营业状况分布图")+

     theme(

          title=element_text(family="myFont"),

          panel.grid = element_blank(),

          panel.background = element_blank(),

          axis.text = element_blank(),

          axis.ticks = element_blank(),

          axis.title = element_blank(),

          legend.position =c(0.15,0.4),

          legend.background=element_rect(colour="white",fill="white"),

          legend.text.align=1

          )




魔方学院QQ群:


QQ群:

微信群:



100 23215 100 23215 0 0 6624 0 0:00:03 0:00:03 --:--:-- 6623 * Connection #0 to host 37.48.118.90 left intact

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存