R软件的ggplot包升级以后有绘制地图的新功能,其图形元素主要是通过geom_map来实现。由于系统内maps包所自带的地图数据没有广州市的数据。其它图家地图数据则要从外部导入,本文则尝试从外部导入广州数据,然后用ggplot2包进行绘图。
本例的目的是结合地理数据和房价数据,以说明广州市房价的行政区分布情况。基本步骤是先导入地理信息数据,将其转为ggplot2可以处理的数据框格式,将房价数据整合进来,最后绘制成图。在下图中,颜色越深代表房价越高。由于本人只在网络上下载到旧版的广州市地图,所以与现实是有些出入的。广州市行政区gis图shp格式.rar
接下来展示本案例的代码
library(maptools);
library(ggplot2)
library(plyr)
gzmap<-readShapePoly('C:/Users/Thinkpad/Desktop/广州市行政区划/guangzhou.shp')
head(fortify(gzmap))
ggplot(gzmap,aes(x=long,y=lat,group=group))+geom_path()
gzfj<-data.frame(行政区=c('白云区','海珠区','黄埔区','荔湾区','天河区','越秀区','花都','南沙区',
'增城','从化','番禺区','萝岗区'),
房价=c(14740,17476,12387,16123,20319,26176,8541,9062,8520,8636,13838,14555))
gz.map<-fortify(gzmap,gzmap$NAME_EN)
gzdata<-data.frame(id =unique(gz.map$id),name=gzmap$NAME_EN)
gzdata1<-merge(gzfj,gzdata,by.x='行政区',by.y='name')
gz_map<-merge(gz.map,gzdata1,by.x='id',by.y='id')
gz_map<-arrange(gz_map,group,order)
head(gz_map)
ggplot(gz_map,aes(map_id = id))+
geom_map(aes(fill = 房价), map =gz.map,colour='black') +
expand_limits(x = gz.map$long, y = gz.map$lat)+theme(
panel.grid=element_blank(),
panel.background=element_blank(),
axis.text=element_blank(),
axis.title=element_blank())
ggplot(gz_map,aes(map_id = id))+
geom_map(aes(fill = 房价),map =gz.map,colour='black') +
expand_limits(x = gz.map$long, y = gz.map$lat)+
ggtitle("广州房价分布图")+
scale_fill_gradient2(low='#F0F5FD',mid='#85B0F2',high='#0808E7',midpoint=median(gzfj$房价))+
theme(panel.grid=element_blank(),
panel.background=element_blank(),
axis.text=element_blank(),
axis.title=element_blank())
ggplot(gz_map,aes(map_id = id))+
geom_map(aes(fill = 房价),map =gz.map,colour='black') +
expand_limits(x = gz.map$long, y = gz.map$lat)+
ggtitle("广州房价分布图")+
scale_fill_gradient(high = "darkblue",low = "lightblue")+
theme(panel.grid=element_blank(),
panel.background=element_blank(),
axis.text=element_blank(),
axis.title=element_blank())
gzmap<-readShapePoly('C:/Users/Thinkpad/Desktop/广州市行政区划/guangzhou.shp')
gz.map<-fortify(gzmap,gzmap$NAME_EN)
gzfj<-data.frame(行政区=c('白云区','海珠区','黄埔区','荔湾区','天河区','越秀区','花都','南沙区',
'增城','从化','番禺区','萝岗区'),
房价=c(14740,17476,12387,16123,20319,26176,8541,9062,8520,8636,13838,14555))
gzdata1<-merge(gzfj,gzdata,by.x='行政区',by.y='name')
str(gzdata1)
gzdata1$long[1]<-mean(gz.map$long[gz.map$id==gzdata1$id[1])
gzdata1$long[2]<-mean(gz.map$long[gz.map$id==gzdata1$id[2])
gzdata1$long[3]<-mean(gz.map$long[gz.map$id==gzdata1$id[3])
gzdata1$long[4]<-mean(gz.map$long[gz.map$id==gzdata1$id[4])
gzdata1$long[5]<-mean(gz.map$long[gz.map$id==gzdata1$id[5])
gzdata1$long[6]<-mean(gz.map$long[gz.map$id==gzdata1$id[6])
gzdata1$long[7]<-mean(gz.map$long[gz.map$id==gzdata1$id[7])
gzdata1$long[8]<-mean(gz.map$long[gz.map$id==gzdata1$id[8])
gzdata1$long[9]<-mean(gz.map$long[gz.map$id==gzdata1$id[9])
gzdata1$long[10]<-mean(gz.map$long[gz.map$id==gzdata1$id[10])
gzdata1$long[11]<-mean(gz.map$long[gz.map$id==gzdata1$id[11])
gzdata1$long[12]<-mean(gz.map$long[gz.map$id==gzdata1$id[12])
gzdata1$long[13]<-mean(gz.map$long[gz.map$id==gzdata1$id[13])
gzdata1$long[14]<-mean(gz.map$long[gz.map$id==gzdata1$id[14])
gzdata1$lat[1]<-mean(gz.map$lat[gz.map$id==gzdata1$id[1])
gzdata1$lat[2]<-mean(gz.map$lat[gz.map$id==gzdata1$id[2])
gzdata1$lat[3]<-mean(gz.map$lat[gz.map$id==gzdata1$id[3])
gzdata1$lat[4]<-mean(gz.map$lat[gz.map$id==gzdata1$id[4])
gzdata1$lat[5]<-mean(gz.map$lat[gz.map$id==gzdata1$id[5])
gzdata1$lat[6]<-mean(gz.map$lat[gz.map$id==gzdata1$id[6])
gzdata1$lat[7]<-mean(gz.map$lat[gz.map$id==gzdata1$id[7])
gzdata1$lat[8]<-mean(gz.map$lat[gz.map$id==gzdata1$id[8])
gzdata1$lat[9]<-mean(gz.map$lat[gz.map$id==gzdata1$id[9])
gzdata1$lat[10]<-mean(gz.map$lat[gz.map$id==gzdata1$id[10])
gzdata1$lat[11]<-mean(gz.map$lat[gz.map$id==gzdata1$id[11])
gzdata1$lat[12]<-mean(gz.map$lat[gz.map$id==gzdata1$id[12])
gzdata1$lat[13]<-mean(gz.map$lat[gz.map$id==gzdata1$id[13])
gzdata1$long[14]<-mean(gz.map$long[gz.map$id==gzdata1$id[14])
ggplot(gz_map,aes(map_id = id))+
geom_map(aes(fill = 房价),map =gz.map,colour='black') +
expand_limits(x = gz.map$long, y = gz.map$lat)+
ggtitle("广州房价分布图")+
scale_fill_gradient2(low='#F0F5FD',mid='#85B0F2',high='#0808E7',midpoint=median(gzfj$房价))+
geom_text(aes(x =long,y = lat,label = 行政区), family = "GB1", data = gzdata1)+
theme(panel.grid=element_blank(),
panel.background=element_blank(),
axis.text=element_blank(),
axis.title=element_blank())