跟着Nature学作图:R语言ggplot2散点图并给指定的点添加颜色

论文

Telomere-to-mitochondria signalling by ZBP1 mediates replicative crisis

https://www.nature.com/articles/s41586-023-05710-8

s41586-023-05710-8.pdf

大部分图的原始数据都有,争取把有原始数据的图都用R语言来复现一下

41586_2023_5710_MOESM4_ESM (1).xlsx

今天的推文复现一下论文中的Fig1a

图片
image.png

部分示例数据

图片
image.png

最基本的散点图

library(tidyverse)
fig1a<-read_delim("data/20230521/Figure1a.txt",
           delim = " ")

library(ggplot2)

ggplot()+
  geom_point(data=fig1a,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2)),
             size=10,
             shape=21,
             fill="#f1f1f1",
             color="black")+
  theme_bw()
图片
image.png

给指定的点映射颜色

这里我的处理方式是把想要映射颜色的点单独挑出来,然后再叠加一层

geneSelected<-c("ZBP1","IFNB1","CGAS","IFNAR1","STING","IFNAR2")
match(geneSelected,fig1a %>% pull(Gene))

本来是想用上面的代码把图例基因的数据匹配出来,但是有些基因名没有找到,这里我就随机选择几个了

fig1a %>% 
  sample_n(6) -> fig1adf


ggplot()+
  geom_point(data=fig1a,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2)),
             size=10,
             shape=21,
             fill="#f1f1f1",
             color="black")+
  theme_bw()+
  geom_point(data=fig1adf,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2),
                 fill=Gene),
             size=10,
             shape=21)
图片
image.png

接下来是调整一些细节

ggplot()+
  geom_point(data=fig1a,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2)),
             size=10,
             shape=21,
             fill="#f1f1f1",
             color="black")+
  theme_classic()+
  geom_point(data=fig1adf,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2),
                 fill=Gene),
             size=10,
             shape=21)+
  scale_x_continuous(breaks = c(2,3,4))+
  scale_y_continuous(breaks = c(2,3,4))+
  theme(legend.position = "top",
        legend.text = element_text(face="italic"))+
  guides(fill=guide_legend(ncol = 2,title = NULL))+
  labs(x="log2[fold change]nReplicate 1",
       y="log2[fold change]nReplicate 2")
图片
image.png

这里既要把2设置成下标,又要实现文本分行,我暂时想不到用什么办法来实现了

最后是生成封面图的代码

ggplot()+
  geom_point(data=fig1a,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2)),
             size=5,
             shape=21,
             fill="#f1f1f1",
             color="black")+
  theme_classic()+
  geom_point(data=fig1adf,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2),
                 fill=Gene),
             size=5,
             shape=21)+
  scale_x_continuous(breaks = c(2,3,4))+
  scale_y_continuous(breaks = c(2,3,4))+
  theme(legend.position = "top",
        legend.text = element_text(face="italic"))+
  guides(fill=guide_legend(ncol = 2,title = NULL))+
  labs(x="log2[fold change]nReplicate 1",
       y="log2[fold change]nReplicate 2") -> p1

ggplot()+
  geom_point(data=fig1a,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2)),
             size=5,
             shape=21,
             fill="#f1f1f1",
             color="black")+
  theme_classic()+
  geom_point(data=fig1adf,
             aes(x=log2(FC_Replicate_1),
                 y=log2(FC_Replicate_2),
                 fill=Gene),
             size=5,
             shape=21)+
  scale_x_continuous(breaks = c(2,3,4))+
  scale_y_continuous(breaks = c(2,3,4))+
  theme(legend.position = "top",
        legend.text = element_text(face="italic"))+
  guides(fill=guide_legend(ncol = 2,title = NULL))+
  labs(x="log2[fold change]nReplicate 1",
       y="log2[fold change]nReplicate 2")+
  scale_fill_manual(values = c("#ef7a79","#edd08e","#29b473",
                               "#000000","#094c8b","#92d4f6")) -> p2

library(patchwork)
p1+p2
图片
image.png

推文记录的是自己的学习笔记,内容可能会存在错误,请大家批判着看,欢迎大家指出其中的错误

示例数据和代码可以给推文点赞,然后点击在看,最后留言获取

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/28267.html

联系我们
联系我们
分享本页
返回顶部