R绘制中国地图热图

尝试用R制作中国地图的热图

library(spatial)

library(rgdal)

library(raster)

library(RColorBrewer)

library(mapproj)

china_map <- readOGR('/Volumes/china_map/bou2_4p.shp')

x=china_map@data

xs=data.frame(x,id=seq(0:924)-1)

这里考虑到我电脑上输出的省份名字都是乱码的,重新添加了一列,可以参考网站:https://bbs.pinggu.org/forum.php?mod=viewthread&tid=5437964

xs$NAME1=iconv(xs$NAME,'GBK','UTF-8') 

china_map1=fortify(china_map)

china_map_data=join(china_map1,xs,type='full')

unique(china_map_data$NAME1)

mydata=read.csv('/Volumes/hz_prov.csv',fileEncoding = 'GBK')

head(mydata)

china_data=join(china_map_data,mydata,type='full')  #通过NAME1进行合并

ggplot(china_data, aes(x = long, y = lat, group = group,fill = Number)) +

geom_polygon(colour="grey40") +

scale_fill_gradient(low="white",high="red") +  #指定渐变填充色,可使用RGB, steelblue

coord_map("polyconic") +    #指定投影方式为polyconic,获得常见视角中国地图

theme(            #清除不需要的元素

panel.grid = element_blank(),

panel.background = element_blank(),

axis.text = element_blank(),

axis.ticks = element_blank(),

axis.title = element_blank(),

legend.position = c(0.2,0.3)

)


参考制作热图的网站如下:

http://blog.sina.com.cn/s/blog_6bc5205e0102vma9.html

https://cloud.tencent.com/developer/article/1477260

https://www.jianshu.com/p/6a4ba41621d6

此外,还可按照数量分组进行上色:

https://zhuanlan.zhihu.com/p/104584424?utm_source=qq

你可能感兴趣的:(R绘制中国地图热图)