论文原文
加载R包
代码语言:javascript复制library(tidyverse)
library(readxl)
library(sf)
library(ggspatial)
install.packages("devtools")
devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)
library(patchwork)
导入数据
代码语言:javascript复制df <- read_excel("41467_2023_42142_MOESM8_ESM.xlsx") %>%
select(1,2,3,4,5,6,7)
地图绘图
代码语言:javascript复制p1 <- df %>% select(4,5,6) %>%
mutate(Global="Global") %>%
make_long(.,Global,Continent,LandCoverType,Habitat) %>%
ggplot(.,aes(x = x, next_x = next_x,node = node,
next_node = next_node,
label = node))
geom_sankey(flow.alpha = .6,node.fill="#87CEEB",
flow.fill="grey80",width=0.2)
geom_sankey_text(size = 3,color="black")
scale_fill_manual(values="grey80")
theme_sankey(base_size =15)
labs(x = NULL)
scale_x_discrete(position = "top")
theme(legend.position = "none",
axis.text=element_text(color="black"))
绘制桑基图
代码语言:javascript复制❝桑吉图节点的顺序可根据出现的频率通过定义因子的方式来调整顺序 ❞
p2 <- map_data("world") %>% ggplot()
geom_polygon(aes(x=long,y=lat,group=group),fill="grey")
geom_point(data=df,aes(longitude,latitude,fill=Continent,color=Continent),
show.legend=F,size=1)
annotation_north_arrow(location="bl",pad_x=unit(0.03,"in"),pad_y=unit(3.5,"in"),
style=north_arrow_nautical(fill=c("grey40","white"),
line_col="grey20"))
annotate("rect", xmin =-128,xmax =-173, ymin=15,ymax=43, alpha = 0.2,fill = "blue")
annotate("text",x=-150,y=30,hjust=0.5,size=3,color="black",
label="North American5734 samplesn11 cover typesn10 habitats")
theme(plot.background = element_blank(),
panel.background = element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank())
拼图
代码语言:javascript复制p1/p2 plot_layout(height = c(1.5,2))