跟着Nature学作图:R语言ggplot2箱线图叠加蜂群图完整示例

分享
手机软件开发 2024-9-9 12:39:12 24 0 来自 中国
论文

Graph pangenome captures missing heritability and empowers tomato breeding
https://www.nature.com/articles/s41586-022-04808-9#MOESM8
没有找到论文里的作图的代码,但是找到了部门做图数据,我们可以用论文中提供的原始数据模仿出论文中的图
本日的推文重复一下论文中的 Figure4b  Figure4c 箱线图叠加蜂群图
Figure4b的部门数据截图

读取数据

library(readxl)dat.fig4b<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",                      sheet = "Fig4b",                      skip = 1)head(dat.fig4b)作图代码

(ggplot2)library(latex2exp)library(ggbeeswarm)segment.data<-data.frame(x=c(0.8,1.8,2.8),                         xend=c(1.2,2.2,3.2),                         y=c(73,97,83)+1,                         yend=c(73,97,83)+1)ggplot(data=dat.fig4b,aes(x=VarID,                          y=`Standardized gene expression`,                          color=Genotype))+  geom_boxplot(show.legend = FALSE)+  geom_beeswarm(dodge.width = 0.8,shape=21)+  theme_bw()+  theme(panel.grid = element_blank(),        legend.position = c(0.1,0.95),        legend.title = element_blank(),        legend.background = element_rect(fill="transparent"),        legend.key = element_rect(fill="transparent"))+  scale_y_continuous(breaks = c(-50,0,50,100),                     limits = c(-50,100))+  scale_x_discrete(labels=paste0("SV",unique(dat.fig4b$VarID)))+  labs(x=NULL)+  annotate(geom = "text",x=0.8,y=70,label="(n=177)")+  annotate(geom = "text",x=1.2,y=45,label="(n=9)")+  annotate(geom = "text",x=1.8,y=60,label="(n=174)")+  annotate(geom = "text",x=2.2,y=95,label="(n=14)")+  annotate(geom = "text",x=2.8,y=60,label="(n=155)")+  annotate(geom = "text",x=3.2,y=80,label="(n=134)")+  geom_segment(data=segment.data,               aes(x=x,xend=xend,y=y,yend=yend),               inherit.aes = FALSE)+  annotate(geom = "text",x=1,y=76,           label=TeX(r"(\textit{P} = 0.76)"),vjust=0)+  annotate(geom = "text",x=2,y=99.5,           label=TeX(r"(\textit{P} = 8.37 \times 10${^-}{^3}$)"),vjust=0)+  annotate(geom = "text",x=3,y=85.5,           label=TeX(r"(\textit{P} = 6.84 \times 10${^-}{^8}$)"),vjust=0)Figure4c数据的部门截图

读取数据

library(readxl)dat.fig4c<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",                      sheet = "Fig4c",                      skip = 1)head(dat.fig4c)作图代码

dat.fig4c$Type<-factor(dat.fig4c$Type,                       levels = c("non-favourable","favourable"))dat.fig4c$Variation<-factor(dat.fig4c$Variation,                       levels = c("SV2","SV4","SV2+SV4"))x<-c(0.7,1.3,1.7,2.3,2.7,3.3)y<-c(0.24,0.42,0.15,0.42,0.24,0.42)label_z<-c(" = 174)"," = 14)"," = 155)"," = 34)"," = 178)"," = 10)")ggplot(data = dat.fig4c,       aes(x=Variation,y=BLUP))+  geom_boxplot(aes(color=Type),show.legend = FALSE)+  geom_beeswarm(aes(color=Type),                dodge.width = 0.8,shape=21)+  scale_color_manual(values = c("#648fff","#d36b1c"),                     name="",                     label=c("Non-favourable alleles",                             "Favourable alleles"))+  scale_x_discrete(label=c("SV2_44168216",                           "SV4_54067283",                           "Both"))+  labs(x=NULL,y="BLUP of SSC")+  theme_bw()+  theme(panel.grid = element_blank(),        legend.position = "bottom",        legend.justification = c(0,0)) -> p2.1for (i in 1:6){  p2.1+    annotate(geom = "text",x=x,y=y,label=TeX(r"((\textit{n})"),             vjust=0,hjust=1) -> p2.1}for (i in 1:6){  p2.1+    annotate(geom = "text",x=x,y=y,label=label_z,             vjust=0,hjust=0) -> p2.1}p2.1末了是拼图

library(patchwork)p1+p2.1+  theme(legend.position = "none")
示例数据和代码可以本身到论文中获取,大概给本篇推文点赞,点击在看,然后留言获取
欢迎各人关注我的公众号
小明的数据分析条记本
小明的数据分析条记本 公众号 重要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物干系转录组学、基因组学、群体遗传学文献阅读条记;3、生物信息学入门学习资料及本身的学习条记!
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-19 11:53, Processed in 0.160547 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表