跟着Nature Plants学作图:R语言ggplot2画变种火山图

论文

The flying spider-monkey tree fern genome provides insights into fern evolution and arborescence

https://www.nature.com/articles/s41477-022-01146-6#Sec44

数据下载链接

https://doi.org/10.6084/m9.figshare.19125641

今天的推文重复一下论文中的Extended Data Fig. 3 c

image.png

他这个图的数据是怎么算出来的我还有点搞不明白,它的图注的内容也没有看明白

Gene pairs plotted according to log2 fold change (L2F) as calculated for gene 1 (x-axis) and gene 2 (y-axis)
in DESeq2. Each point represents one gene pair with pairs colored according to the difference in L2F values (diffL2F = |L2F_1 - L2F_2|) to visualize the
arbitrary cutoffs of diffL2F = 2 and diffL2F = 4.

部分示例数据如下

image.png

作图数据是 L2F_1 和 L2F_2 两列,根据L2F_diff的值需要增加一列映射颜色

首先是读取数据

library(readxl)
dat01<-read_excel("data/20220529/20220529.xlsx")
head(dat01)

增加一列映射颜色

library(tidyverse)

dat01 %>% 
  mutate(diffL2F=case_when(
    L2F_diff < 2 ~ "<2",
    L2F_diff >=2 & L2F_diff<=4 ~ ">2",
    TRUE ~ ">4"
    )) -> dat01.1

作图代码

library(ggplot2)

ggplot(data=dat01.1,aes(x=L2F_1,y=L2F_2))+
  geom_point(aes(color=diffL2F))+
  scale_color_manual(values = c("<2"="#7f7f7f",
                                ">2"="#fe0904",
                                ">4"='#f9b54f'))+
  geom_abline(intercept = 0,slope = 1,
              lty="dashed",size=1,
              color="blue")
image.png

论文中有六组数据,批量读入,批量作图

批量读取excel

library(tidyverse)
library(readxl)
list.files("data/20220529/",
           pattern = "*.xlsx",
           full.names = TRUE) %>% 
  map(.,read_excel) -> dat.list

批量作图

library(ggplot2)
plot.list = list()

text.label<-c("StGa","SoGa","LeGa","StSo","SoLe","LeSt")

for (i in 1:6){
  dat.list[[i]] %>% 
    mutate(diffL2F=case_when(
      L2F_diff < 2 ~ "<2",
      L2F_diff >=2 & L2F_diff<=4 ~ ">2",
      TRUE ~ ">4"
    )) %>% 
    ggplot(aes(x=L2F_1,y=L2F_2))+
    geom_point(aes(color=diffL2F))+
    scale_color_manual(values = c("<2"="#7f7f7f",
                                  ">2"="#fe0904",
                                  ">4"='#f9b54f'))+
    geom_abline(intercept = 0,slope = 1,
                lty="dashed",size=1,
                color="blue")+
    geom_text(aes(x=-Inf,y=Inf),
              hjust=-0.5,vjust=2,
              label=text.label[i])+
    labs(x=NULL,y=NULL) -> plot.list[[i]]
}

将六个图拼接到一起

wrap_plots(plot.list,ncol=3,nrow=2,byrow = TRUE)+
  plot_layout(guides = "collect") -> p1
p1
image.png

修改整体的边界空白

p1 +
  plot_annotation(theme = 
                    theme(plot.margin = unit(c(0.2,0.2,1.2,1.2),'cm')))

添加坐标轴标题

grid::grid.draw(grid::textGrob("Log2(fold change)\ngene1", x = 0.04, rot = 90))
grid::grid.draw(grid::textGrob("Log2(fold change)\ngene2", y = 0.04))
image.png

示例数据可以到论文中去下载,代码可以直接在推文中复制,如果需要我整理好的示例数据和代码,可以给推文打赏1元获取

欢迎大家关注我的公众号

小明的数据分析笔记本

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

你可能感兴趣的:(跟着Nature Plants学作图:R语言ggplot2画变种火山图)