-
地图数据获得
链接:https://pan.baidu.com/s/1JKD3IUONtKGO5tXMVBrTyA
提取码:data -
绘制地图
# 数据读入
library(sf)
library(ggspatial)
library(ggplot2)
library(cowplot)
library(colorspace)
# 读入地图绘制数据,这里是各个地区的污染物浓度,需要包含geometry格式数据
df = read.csv('G:/Desktop/data_map.csv', encoding='UTF-8')
df = st_as_sf(df, wkt='geometry', crs=4326)
# 读入中国区县分布数据
china = sf::read_sf('G:/Desktop/中国省级地图GS(2019)1719号.geojson')
# 读入九段线数据
nine_lines = sf::read_sf('G:/Desktop/九段线GS(2019)1719号.geojson')
# 绘制中国地图
fig=ggplot() +
geom_sf(data=china, fill='NA', size=0.5, color='grey') + # 中国地图轮廓
geom_sf(data = df,aes(color=PM25), size=1) + # 九段线轮廓
scale_color_continuous_sequential(palette = "YlOrRd", rev = T, name=expression('PM'[2.5]*'浓度(ug/m'^3*')')) + # 地区污染物分布数据,设置配色板
coord_sf(ylim = c(-2387082,1654989), crs='+proj=laea +lat_0=40 +lon_0=104') + #截取中国地图,不在大图上显示九段线区域
# 添加比例尺
annotation_scale(location='bl') +
# 添加指北针
annotation_north_arrow(location='tl', which_north='false',
style=north_arrow_nautical()) +
# 设置大图边框
theme(rect=element_blank(),
panel.background=element_blank(),
panel.grid=element_blank(),
axis.line=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
legend.key.size = unit(10, "pt"),
legend.title = element_text(size = 9))
# 绘制九段线小图
nine_map = ggplot() +
geom_sf(data = china,fill='NA', size=0.5) +
geom_sf(data = nine_lines,color='black',size=0.5)+
coord_sf(ylim = c(-4028017,-1877844),xlim = c(117131.4,2115095),crs="+proj=laea +lat_0=40 +lon_0=104")+
theme(
aspect.ratio = 1.25, #调节长宽比
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
panel.background = element_blank(),
panel.border = element_rect(fill=NA,color="grey10",linetype=1,size=0.5),
plot.margin=unit(c(0,0,0,0),"mm"))
# 使用cowplot包将大图小图拼在一起
fig = ggdraw() +
draw_plot(fig) +
draw_plot(nine_map, x = 0.8, y = 0, width = 0.13, height = 0.39)
# 保存图片为png格式
png('G:/Desktop/PM25分布图.png',bg="transparent",
width = 1200, height = 800, res=200)
# 显示图片(必需)
fig
# 关闭画框(必需)
dev.off()
实例