跟着Nature Ecology&Evolution学作图:R语言ggplot2世界地图/柱形图/组合图

2023-11-30 18:09:53 浏览数 (2)

论文

Meta-analysis reveals less sensitivity of non-native animals than natives to extreme weather worldwide

https://www.nature.com/articles/s41559-023-02235-1

论文中提供作图用到的原始数据,我们可以试着复现一下论文中的图。今天的推文试着复现一下论文中的figure1

image.png

先画一个地图

代码语言:javascript复制
library(tidyverse)
library(readxl)

world.dat<-map_data("world")

ggplot()  
  geom_polygon(data=world.dat,aes(x=long,y=lat,group=group),
               fill="#dedede") 
  theme_bw() 
  scale_y_continuous(expand = expansion(mult=c(0,0))) 
  scale_x_continuous(expand = expansion(add=c(0,0))) 
  theme(panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) 
  labs(x=NULL,y=NULL)-> world.map

world.map

figure1a代码添加采样地点

代码语言:javascript复制
fig1a.dat<-read_excel("data/20231127/41559_2023_2235_MOESM8_ESM.xlsx",
                      sheet = "Fig1A")
fig1a.dat


world.map 
  geom_point(data=fig1a.dat,aes(x=Longitude,y=Latitude,color=sufferedECE)) 
  scale_color_manual(values = c("Cold"="#b7f8ff",
                                "Drought"="#ae965b",
                                "Flood"="#4d4dff",
                                "Heatwave"="#ff6f6f",
                                "Storm"="#ffa64d"),
                     name="EWEs") 
  guides(color=guide_legend(override.aes = list(size=5))) 
  theme(legend.position = c(0.1,0.3))

image.png

figure1b 代码和1a基本一致

代码语言:javascript复制
fig1b.dat<-read_excel("data/20231127/41559_2023_2235_MOESM8_ESM.xlsx",
                      sheet = "Fig1B")
fig1b.dat


world.map 
  geom_point(data=fig1b.dat,aes(x=Longitude,y=Latitude,color=sufferedECE)) 
  scale_color_manual(values = c("Cold"="#b7f8ff",
                                "Drought"="#ae965b",
                                "Flood"="#4d4dff",
                                "Heatwave"="#ff6f6f",
                                "Storm"="#ffa64d"),
                     name="EWEs") 
  guides(color=guide_legend(override.aes = list(size=5))) 
  theme(legend.position = "none")

image.png

figure1c d

代码语言:javascript复制

fig1c.dat<-read_excel("data/20231127/41559_2023_2235_MOESM8_ESM.xlsx",
                      sheet = "Fig1C") %>% 
  mutate(sufferedECE=factor(sufferedECE,levels = c("Heatwave","Cold","Storm","Flood","Drought")))
fig1c.dat

pc<-ggplot(data=fig1c.dat,aes(x=sufferedECE,y=number_effct_sizes)) 
  geom_col(aes(fill=sufferedECE)) 
  scale_fill_manual(values = c("Cold"="#b7f8ff",
                                "Drought"="#ae965b",
                                "Flood"="#4d4dff",
                                "Heatwave"="#ff6f6f",
                                "Storm"="#ffa64d"),
                     name="EWEs") 
  theme_bw() 
  theme(legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line = element_line(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) 
  scale_y_continuous(limits = c(0,500),
                     breaks = seq(0,500,by=100),
                     labels = c(0,100,"",300,"",500),
                     expand = expansion(mult = c(0,0))) 
  geom_text(aes(label=c(84,47,31,24,21)),vjust=-1) 
  annotate(geom = "text",x=2,y=350,label="Number of studies",hjust=0) 
  annotate(geom = "segment",x=2,y=330,xend = 2,yend=250,
           arrow=arrow(angle = 20,type="closed",length = unit(3,'mm'))) 
  labs(x=NULL,y="Number of sampleneffect sizes")
pc

fig1d.dat<-read_excel("data/20231127/41559_2023_2235_MOESM8_ESM.xlsx",
                      sheet = "Fig1D") %>% 
  mutate(sufferedECE=factor(sufferedECE,levels = c("Heatwave","Cold","Storm","Flood","Drought")))
fig1d.dat

pd<-ggplot(data=fig1d.dat,aes(x=sufferedECE,y=number_effct_sizes)) 
  geom_col(aes(fill=sufferedECE)) 
  scale_fill_manual(values = c("Cold"="#b7f8ff",
                               "Drought"="#ae965b",
                               "Flood"="#4d4dff",
                               "Heatwave"="#ff6f6f",
                               "Storm"="#ffa64d"),
                    name="EWEs") 
  theme_bw() 
  theme(legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line = element_line(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) 
  scale_y_continuous(limits = c(0,2000),
                     breaks = seq(0,2000,by=500),
                     labels = c(0,500,"",1500,""),
                     expand = expansion(mult = c(0,0))) 
  geom_text(aes(label=c(74,32,129,47,76)),vjust=-1) 
  labs(x=NULL,y="Number of sampleneffect sizes")

pd

pc pd

image.png

figure1e

代码语言:javascript复制
fig1e.dat<-read_excel("data/20231127/41559_2023_2235_MOESM8_ESM.xlsx",
                      sheet = "Fig1E") %>% 
  rename("group"="...1") %>% 
  pivot_longer(!group)

fig1e.dat

pe<-ggplot(data=fig1e.dat,aes(x=name,y=value)) 
  geom_bar(stat="identity",
           aes(fill=group),
           position = "dodge") 
  theme_bw() 
  theme(panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line.y = element_line(),
        axis.ticks.x = element_blank(),
        axis.text.x = element_text(angle=90,hjust=1),
        legend.position = c(0.8,0.8)) 
  scale_y_continuous(limits = c(0,36),
                     expand = expansion(mult=c(0,0)),
                     breaks = seq(0,35,by=5),
                     labels = c(0,5,"",15,"",25,"",35)) 
  scale_fill_manual(values = c("nonnative_prop"="#7fb3ff",
                               "native_prop"="#00008a"),
                    name="",
                    labels=c("Native","Non-native")) 
  labs(x=NULL,y="Proportion of samplen(effect sizes(%)")
pe

image.png

5个图组合到一起

代码语言:javascript复制
part1<-pa pc pb pd 
  plot_layout(ncol = 2,nrow=2,widths = c(3,1))

part1/pe 
  plot_layout(nrow=2,heights = c(2,1)) 
  plot_annotation(tag_levels = 'a') 

image.png

示例数据可以到论文中下载,或者给推文打赏1元获取我整理的示例数据和代码

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

image.png

0 人点赞