其他
R绘图模板——世界地图的绘制及拓展!!!
】
点击上方
“科研后花园”
关注我们
主要代码如下:
rm(list = ls())
setwd("D:/桌面/世界地图绘制")
#加载R包
library(ggplot2)
2、加载绘图世界地图的数据(这里按理说应该加载maps包,但是好像加载ggplot2包即可导入数据,没搞明白):df_map <- map_data("world")
3、绘制基础世界地图:##绘制基础世界地图
p <- ggplot(df_map,aes(long, lat, group = group)) +
geom_polygon()+
borders("world",regions = ".",color = "grey50",fill="white",size=0.5)+
#主题设置
theme_void()
p
p+geom_polygon(aes(fill=region),show.legend = F)+
theme(plot.background = element_rect(fill="#76daff"))
p+geom_polygon(aes(fill=order))
p+coord_map("ortho")
#构造采样点的经纬度
df_sample1 <- data.frame(
lon=runif(12, min = 0, max = 120),
lat=runif(12, min = 25, max = 45),
group=rep(c('1980','2000','2005','2010'),times=c(3,3,3,3)))
df_sample2 <- data.frame(
lon=runif(12, min = -125, max = -75),
lat=runif(12, min = 30, max = 65),
group=rep(c('1980','2000','2005','2010'),times=c(1,5,4,2)))
df_sample3 <- data.frame(
lon=runif(12, min = -5, max = 20),
lat=runif(12, min = 10, max = 25),
group=rep(c('1980','2000','2005','2010'),times=c(2,5,3,2)))
#合并数据
df_sample <- rbind(df_sample1,df_sample2,df_sample3)
#顺便统计出各组的数量
data <- data.frame(group=c('1980','2000','2005','2010'),
value=c(6,13,10,7))
##绘制子图
p2 <- ggplot(data,aes(group,value,fill=group))+
geom_col()+
scale_fill_manual(values = c("#00c700", "#da0000","#0054da","#6a3d00"))+
theme_classic()+
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.text.y = element_text(color = "black",size=12),
axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
panel.background = element_blank(),
plot.background = element_blank())+
labs(x='', y='')+
scale_y_continuous(expand = c(0,0))
#添加采样点信息
p+geom_point(df_sample,
mapping=aes(lon, lat, fill = group),
size=3,shape=21,color="black",
show.legend = T)+
scale_fill_manual(name=NULL,values = c("#00c700", "#da0000","#0054da","#6a3d00"))+
annotation_custom(grob=ggplotGrob(p2),
ymin = -75, ymax = -30,
xmin=-190, xmax=-80)+
theme(legend.position = c(0.1,0.5))
PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
完整代码(附带注释)及原始数据获取:
夸克网盘(手机端下载夸克APP进行搜索提取):
链接:https://pan.quark.cn/s/053edd38709c
提取码:Rznq