加载必要的库

library(rgdal) library(RColorBrewer) library(classInt) library(maptools) library(ggplot2)

读取行政区划数据

cityname <- readOGR('path/to/shapefile','chengdu')

设置颜色和图例

colors <- brewer.pal(25,'RdYlBu') class <- classIntervals(cityname$District, n=25, style='quantile') legend_title <- 'District' brks <- class$brks

绘制地图

ggplot() + geom_polygon(data=cityname, aes(x=long, y=lat, group=group, fill=cut(District, breaks=brks))) + coord_equal() + scale_fill_manual(values=colors, breaks=brks, drop=FALSE) + labs(title='成都市', fill=legend_title) + theme(plot.title=element_text(hjust=0.5), legend.justification=c(1,1), legend.position=c(1,1), legend.title=element_text(size=10), legend.text=element_text(size=8), legend.key.width=unit(0.85,'cm'), axis.text.x=element_text(angle=45, hjust=1)) + guides(fill=guide_legend(title=legend_title, keywidth=0.85, keyheight=0.85, label.position='right', label.theme=element_blank(), nrow=5, byrow=TRUE)) + theme(panel.background=element_rect(fill=NA), panel.border=element_rect(color='darkgrey', size=2, linetype='dashed'), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), axis.line=element_blank(), axis.title=element_blank(), axis.ticks=element_blank(), plot.margin=unit(c(3.1,3.1,2.1,2.1),'cm'))

使用R语言绘制成都市及主城区地图

原文地址: https://www.cveoy.top/t/topic/n9yd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录