本文所使用的代码是之前一篇关于航线图的数据,之所以要从新写一遍,是为了让大家体会借助在线地图制作地图可视化在代码效率上的便利(当然,也会有损失,你不能像操纵shapefile那样对地图的细节元素进行自定义了)。
本文调用得ggmap包,该包封装了包含Googlemap、openstreetmap、stamenmap等强大在线开源地图的地图素材背景,如果能仔细甄别、精心挑选,还是可以淘到不少好的背景的。
因为ggmap是哈神参与创建的包,专门为了拓展ggplot对于地图源的支持,弥补其制作数据地图方面的缺陷,所以我们从ggmap中get到的所有地图素材,都是可以直接供ggplot的图层函数调用的,只是之后的投影参数什么的部分会失效。(其实说起来,它相当于我们在之前的数据地图语法中,可以直接弃用ggplot()+geom_polygon()这两个部分,看过之前使用shp素材制作数据地图的小伙伴儿一定都知道,我们写代码的过程中,一般代码量都贡献给了shp素材的导入、抽取、整理、合并等过程,而调用ggmap的在线地图背景,确实可以省去不少功夫,代码量也小了很多!)。
library("plyr") library("dplyr") library("ggplot2") library("ggmap") library("maptools") library("maps") library("Cairo") library("baidumap")
这是从ggmap包中调用自定义范围和缩放级别的地图素材:
bbox_everest <- c(left =60, bottom =10, right =150, top =60) mapdata<-get_stamenmap(bbox_everest, zoom =5)
城市经纬度素材:
city_list<-c("西安","西宁","郑州","重庆","成都","石家庄","兰州","济南","大同","咸阳","包头") address<-getCoordinate(city_list, formatted = T) address<-data.frame(address,city=row.names(address),stringsAsFactors = FALSE)
del<-getCoordinate("长沙", formatted = T) del<-as.character(del) lonx<-as.numeric(rep(del[1],11)) laty<-as.numeric(rep(del[2],11))
address<-data.frame(address,lonx,laty) address$lonx<-as.numeric(address$lonx) address$laty<-as.numeric(address$laty)
names(address)[1:2]<-c("lon","lat") address$Num<-round(runif(11,50,100),2)
#-------------------------------------------------------------------------------------------------------------
图形代码:(气泡图+放射线图)
CairoPNG(file="C:/Users/Administrator/Desktop/航线图1.png",width=1000,height=670) ggmap(mapdata)+ geom_segment(data=address,aes(x=lon,y=lat,xend=lonx,yend=laty),size=0.3,colour="#FF6833")+ geom_point(data=address,aes(x=lon,y=lat,size=Num),shape=21,fill="#ED7D31",col="#E02939",alpha=.6)+ geom_point(data=NULL,aes(x=112.97935,y=28.21347),shape=21,size=8,fill=NA,col="steelblue")+ scale_size_area(max_size=8)+ theme_nothing() dev.off()
#####################################################################
气泡图+迁徙路径图
city_list<-c("海口","广州","长沙","武汉","郑州","石家庄","北京","沈阳","长春","哈尔滨") addA<-getCoordinate(city_list, formatted = T) addA<-data.frame(addA,city=row.names(addA),stringsAsFactors = FALSE)
data1<-addA[-1,] names(data1)[1:2]<-c("lonx","latx") data2<-addA[-length(row.names(addA)),] names(data2)[1:2]<-c("lony","laty")
addB<-cbind(data2,data1)[,-3]
addA$Num<-round(runif(10,50,100),2) names(addA)[1:2]<-c("lon","lat")
#-------------------------------------------------------------------------------------------------------
CairoPNG(file="C:/Users/Administrator/Desktop/航线图2.png",width=1000,height=670) ggmap(mapdata)+ geom_segment(data=addB,aes(x=lonx,y=latx,xend=lony,yend=laty),size=0.3,colour="#FF6833")+ geom_point(data=addA,aes(x=lon,y=lat,size=Num),shape=21,fill="#ED7D31",col="#E02939",alpha=.6)+ theme_nothing() dev.off()
#------------------------------------------------------------------------------------------------------
气泡图+闭环路径迁徙图
city_list3<-c("兰州","成都","重庆","贵阳","昆明","南宁","海口","广州","福州","上海","青岛","石家庄","呼和浩特","银川") addC<-getCoordinate(city_list3, formatted = T) addC<-data.frame(addC,city=row.names(addC),stringsAsFactors = FALSE) names(addC)[1:2]<-c("lon","lat")
datac1<-addC[2:14,] datac2<-addC[1,] addCC<-rbind(datac1,datac2) adddata<-cbind(addC,addCC) names(adddata)<-c("lonx","latx","city","lony","laty","city") adddata<-adddata[,-3] addC$Num<-round(runif(14,50,100),2)
#-------------------------------------------------------------------------------------------------------------
CairoPNG(file="C:/Users/Administrator/Desktop/航线图3.png",width=1000,height=670) ggmap(mapdata)+ geom_segment(data=adddata,aes(x=lonx,y=latx,xend=lony,yend=laty),size=0.3,colour="#FF6833")+ geom_point(data=addC,aes(x=lon,y=lat,size=Num),shape=21,fill="#ED7D31",col="#E02939",alpha=.6)+ scale_size_area(max_size=8)+ theme_nothing() dev.off()
#-------------------------------------------------------------------------------------------------------------
矩形气泡图:
CairoPNG(file="C:/Users/Administrator/Desktop/航线图4.png",width=1000,height=670) ggmap(mapdata)+ geom_point(data=address,aes(x=lon,y=lat,size=Num,fill=Num),shape=22,col="#E02939",alpha=.6)+ scale_fill_gradient2(low="#8E0F2E", mid="#BFBEBE", high="#0E4E75", midpoint=median(na.omit(address$Num)))+ scale_size_area(max_size=8)+ theme_nothing() dev.off()
#-------------------------------------------------------------------------------------------------------------矩形气泡图:
CairoPNG(file="C:/Users/Administrator/Desktop/航线图5.png",width=1000,height=670) ggmap(mapdata)+ geom_point(data=address,aes(x=lon,y=lat,size=Num,fill=Num),shape=23,col="#E02939",alpha=.6)+ scale_fill_gradient2(low="#8E0F2E", mid="#BFBEBE", high="#0E4E75", midpoint=median(na.omit(address$Num)))+ scale_size_area(max_size=8)+ theme_nothing() dev.off()
最后总结一下关于ggplot调用ggmap制作数据地图的几点建议: