谷歌地圖與ggplot2系統如何無縫對接

本文所使用的代碼是之前一篇關於航線圖的數據,之所以要從新寫一遍,是為了讓大家體會藉助在線地圖製作地圖可視化在代碼效率上的便利(當然,也會有損失,你不能像操縱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製作數據地圖的幾點建議:

  • 弄清業務分析場景:ggmap中的在線地圖素材大多是特定場景素材,有道路圖、河流圖、交通圖、衛星圖、等高線圖、地貌圖植被圖等,只有明確自己的圖表使用場景才能選好素材。

  • 明白圖形使用場景:ggmap的地圖更適合用於電子屏幕演示,接近實景,比較震撼觀眾,但是也因素接近實景,背景太過噪雜,不適宜出版物級別的用途,想要做成的簡潔、清新,還是使用諸如shapefile這素材導入,一點一點兒的調參美容,就跟繡花一樣,效率低,但是效果好。

  • 語法上的差別:單純使用ggplot結合shp素材製作,語法複雜,而藉助ggmap,語法簡潔,其中最重要的差異,我覺得是投影參數,在ggmap中請勿使用地圖投影參數,因為在線地圖本身自帶投影,你不知情的情況下使用的投影格式很可能與素材的投影格式不一致,導致坐標錯位。

聯繫方式:

wechat:ljty1991

Mail:578708965@qq.com

個人公眾號:數據小魔方(datamofang)

團隊公眾號:EasyCharts

qq交流群:[魔方學院]553270834

github:github.com/ljtyduyu/wea-


推薦閱讀:

《R語言實戰》第五部分第十九章-使用ggplot2進行高級繪圖學習筆記
R語言數據清洗實戰——世界瀕危遺產地數據爬取案例
多層數據巧鑽取,業務盲點無處可躲~
珍藏,最全面+最完善的Excel條件格式使用手冊
世紀佳緣用戶畫像-Part1

TAG:R编程语言 | 数据可视化 | 图表 |