ITPub博客

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

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

作者:浪花小雨滴 时间:2020-06-16 15:17:27 0 删除 编辑

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

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

加载包:

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
          )

联系方式:

wechat:ljty1991
Mail:578708965@qq.com
个人公众号:数据小魔方(datamofang)
团队公众号:EasyCharts
qq交流群:[魔方学院]553270834

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3154921/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    39879