3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
ID:FuDanBigData本文轉自數據小魔方,轉載已獲授權。本文所使用的代碼是之前一篇關於航線圖的數據,之所以要從新寫一遍,是為了讓大家體會藉助在線地圖製作地圖可視化在代碼效率上的便利(當然,也會有損失,你不能像操縱shapefile那樣對地圖的細節元素進行自定義了)。本文調用得ggmap包,該包封裝了包含Googlemap、openstreetmap、stamenmap等強大在線開源地圖的地圖素材背景,如果能仔細甄別、精心挑選,還是可以淘到不少好的背景的。因為ggmap是哈神參與創建的包,專門為了拓展ggplot對於地圖源的支持,彌補其製作數據地圖方面的缺陷,所以我們從ggmap中get到的所有地圖素材,都是可以直接供ggplot的圖層函數調用的,只是之後的投影參數什麼的部分會失效。(其實說起來,它相當於我們在之前的數據地圖語法中,可以直接棄用ggplot+geom_polygon這兩個部分,看過之前使用shp素材製作數據地圖的小夥伴兒一定都知道,我們寫代碼的過程中,一般代碼量都貢獻給了shp素材的導入、抽取、整理、合併等過程,而調用ggmap的在線地圖背景,確實可以省去不少功夫,代碼量也小了很多!)。這是從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))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_nothingdev.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_nothingdev.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_nothingdev.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_nothingdev.off弄清業務分析場景:ggmap中的在線地圖素材大多是特定場景素材,有道路圖、河流圖、交通圖、衛星圖、等高線圖、地貌圖植被圖等,只有明確自己的圖表使用場景才能選好素材。明白圖形使用場景:ggmap的地圖更適合用於電子屏幕演示,接近實景,比較震撼觀眾,但是也因素接近實景,背景太過噪雜,不適宜出版物級別的用途,想要做成的簡潔、清新,還是使用諸如shapefile這素材導入,一點一點兒的調參美容,就跟繡花一樣,效率低,但是效果好。語法上的差別:單純使用ggplot結合shp素材製作,語法複雜,而藉助ggmap,語法簡潔,其中最重要的差異,我覺得是投影參數,在ggmap中請勿使用地圖投影參數,因為在線地圖本身自帶投影,你不知情的情況下使用的投影格式很可能與素材的投影格式不一致,導致坐標錯位。回復下列數字即可獲得相應乾貨下載:1:一百多篇大數據文檔下載!2:超全數據分析資料免費下載!(包括SQL,R語言,SPSS,SAS,python,數據分析和數據挖掘)3:清華大學數據科學院講座內容集錦免費下載!4:Python超全資料分享!

本文由yidianzixun提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦