版权声明:该文章来自leeningzzu https://blog.csdn.net/leeningzzu/article/details/88081213
调用R进行作图,所用中国地图模板可从百度网盘下载使用
library("maptools")
china_map=readShapePoly("F:/statistics root/r workspace/Rstudio/china/bou2_4p.shp") #自己的地图模板路径
library(ggplot2)
library(sp)
ggplot(china_map,aes(x=long,y=lat,group=group)) +
geom_polygon(fill="white",colour="grey") +
coord_map("polyconic")
x <- china_map@data #读取行政信息
xs <- data.frame(x,id=seq(0:924)-1) #含岛屿共925个形状
library(ggplot2)
china_map1 <- fortify(china_map) #转化为数据框
library(plyr)
china_map_data <- join(china_map1, xs, type = "full")
library(plyr)
china_map_data <- join(china_map1, xs, type = "full") #合并两个数据框
mydata <- read.csv("F:/statistics root/r workspace/Rstudio/2036english1.csv") #自己的数据
china_data <- join(china_map_data, mydata, type="full")
china_data$ratio <- cut(china_data$ratio20361,breaks=c(1.20,1.25,1.30,1.35,1.40,1.45,1.50),labels=c('1.20~1.25','1.25~1.30','1.30~1.35','1.35~1.40','1.40~1.45','1.45~1.50'),right=FALSE,order=TRUE) #按个人需求连续变量离散化
windowsFonts(myFont = windowsFont("Times New Roman"))
#rhg_cols1 <- c("#FEE0D2","#FCBBA1","#FC9272","#FB6A4A","#EF3B2C") #分等级并填充颜色
rhg_cols1 <- c("#fee0d2","#FCBBA1","#fc9272","#FB6A4A","#ef3b2c","#99000d")
#加载省会坐标
province_city <- read.csv("F:/statistics root/r workspace/Rstudio/chinaprovincecity2.csv") #省会坐标
ggplot(china_data, aes(x = long, y = lat)) +
geom_polygon(aes(group = group, fill = ratio),colour="grey30",size=0.05) +
scale_fill_manual(values=rhg_cols1)+
guides(fill=guide_legend(title='ratio'))+
coord_map("polyconic") +
geom_text(size=1.5,aes(x = jd,y = wd,label = province), hjust=0.5,vjust=0.5,data =province_city)+ #添加省会标签
#清除不必要的背景元素
theme(
panel.grid = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)
作图效果如下