zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-02-19 12:27:38 时间

论文

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

示例数据可以到论文中去下载,代码可以直接在推文中复制