跟着NatureMetabolism学作图:R语言ggplot2转录组差异表达火山图

论文

Independent phenotypic plasticity axes define distinct obesity sub-types

https://www.nature.com/articles/s42255-022-00629-2#Sec15

s42255-022-00629-2.pdf

论文中没有公开代码,但是所有作图数据都公开了,我们可以试着用论文中提供的数据模仿论文中的图

今天的推文重复一下论文中的Fig3b 差异表达火山图,之前也有推文介绍过火山图,今天的推文主要学习的一个知识点是利用latex2exp这个R包添加文本,包括

上下标

换行 换行的基本写法

java 复制代码
ggplot()+
  geom_point(aes(x=1,y=1))+
  labs(x=TeX(r"(\overset${ABCDEF}{abcde}$)"))


火山图的部分示例数据

读取数据

java 复制代码
library(readr)
df<-read_tsv("data/20220921/fig3b.txt")
head(df)
colnames(df)

添加差异表达的分组

java 复制代码
df %>% 
  mutate(change=case_when(
    log2FoldChange > 1 & pvalue < 0.05 ~ "Up",
    log2FoldChange < -1 & pvalue < 0.05 ~ "Down",
    TRUE ~ "Not Sig"
  )) -> new.df

table(new.df$change)
new.df %>% 
  filter(-log10(pvalue)>8) -> new.text.label

这里没有找到论文中差异表达的标准,这里是我随便写的

作图代码

java 复制代码
library(ggplot2)
library(ggrepel)
library(latex2exp)
ggplot(data=new.df,aes(x=log2FoldChange,y=-log10(pvalue)))+
  geom_point(aes(color=change))+
  scale_color_manual(values = c("Down"="#3a53a4",
                                "Not Sig"="#aaaaaa",
                                "Up"="#7acde4"),
                     labels=c("Down"=TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} depleted)"),
                              "Not Sig"="Not Significant",
                              "Up" = TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} enriched)")))+
  theme_classic()+
  theme(legend.position = c(0.2,0.9),
        legend.text.align = 0,
        legend.title = element_blank())+
  geom_text_repel(data=new.text.label,
                  aes(x=log2FoldChange,y=-log10(pvalue),
                      label=mgi_symbol))+
  labs(x=TeX(r"(\overset${\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Lean}$ versus $\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Giant}}{(log{_2}$ fold $change)}$)"),
       y=TeX(r"(-log${_1}{_0}$ {(}\textit{P}{ value}{)})"))


制作封面图

java 复制代码
p1<-ggplot(data=new.df,aes(x=log2FoldChange,y=-log10(pvalue)))+
  geom_point(aes(color=change))+
  scale_color_manual(values = c("Down"="#3a53a4",
                                "Not Sig"="#aaaaaa",
                                "Up"="#7acde4"),
                     labels=c("Down"=TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} depleted)"),
                              "Not Sig"="Not Significant",
                              "Up" = TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} enriched)")))+
  theme_classic()+
  theme(legend.position = c(0.2,0.9),
        legend.text.align = 0,
        legend.title = element_blank())+
  geom_text_repel(data=new.text.label,
                  aes(x=log2FoldChange,y=-log10(pvalue),
                      label=mgi_symbol))+
  labs(x=TeX(r"(\overset${\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Lean}$ versus $\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Giant}}{(log{_2}$ fold $change)}$)"),
       y=TeX(r"(-log${_1}{_0}$ {(}\textit{P}{ value}{)})"))

p2<-ggplot(data=new.df,aes(x=log2FoldChange,y=-log10(pvalue)))+
  geom_point(aes(color=change))+
  scale_color_manual(values = c("Down"="red",
                                "Not Sig"="#aaaaaa",
                                "Up"="darkgreen"),
                     labels=c("Down"=TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} depleted)"),
                              "Not Sig"="Not Significant",
                              "Up" = TeX(r"(\textit{Nnat}${^+}$${^/}$${^-}$${^p}$-\textit{Heavy} enriched)")))+
  theme_classic()+
  theme(legend.position = c(0.2,0.9),
        legend.text.align = 0,
        legend.title = element_blank())+
  geom_text_repel(data=new.text.label,
                  aes(x=log2FoldChange,y=-log10(pvalue),
                      label=mgi_symbol))+
  labs(x=TeX(r"(\overset${\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Lean}$ versus $\textit{Nnat}{^+}{^/}{^-}{^p}-\textit{Giant}}{(log{_2}$ fold $change)}$)"),
       y=TeX(r"(-log${_1}{_0}$ {(}\textit{P}{ value}{)})"))

library(patchwork)
pdf(file = "Rplot03.pdf",width = 14.1,height = 6)
p1+p2
dev.off()

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

相关推荐
白远山19 分钟前
家政服务派单平台搭建实战指南:从需求分析到系统设计全流程解析
java·开发语言·架构·uni-app·需求分析
Tairitsu_H35 分钟前
[C++] C++11右值引用与移动语义揭秘
开发语言·c++·c++11·右值引用·移动语义
麻辣布丁1 小时前
java集合篇面试模拟
java·开发语言·面试
niucloud-admin1 小时前
JAVA V6 多商户商城 开发文档——插件开发规范
java·开发语言
海盗12341 小时前
DSH 接入 OpenCode Go 报 400 MissingSessionID:从 LLM 语义层退到 fetch 传输层的完整排障
开发语言·后端·golang
weixin199701080161 小时前
《Mercari OTA 直连接入:日本二手电商出海的API通道与600次/分钟限流实战》(附Python源码)
开发语言·数据库·python
库玛西2 小时前
深度解构高并发利器:纯事件驱动 Reactor 模式的设计哲学与工程实践
服务器·开发语言·c++·笔记·tcp/ip
lemon_sjdk2 小时前
JavaScript 常用关键字全解析
开发语言·javascript·ecmascript
linx2952 小时前
单元五 · 对称认知·下:编译与链接
linux·c语言·开发语言·c++·嵌入式硬件
2501_930472442 小时前
从物理机到 CVM 全流程落地:部署脚本的 8 个云化改造点(附代码对比)
开发语言·人工智能·架构·腾讯云·perl