跟着Nature学作图:R语言ggplot2箱线图/堆积柱形图完
2022-07-24 本文已影响0人
小明的数据分析笔记本
论文
Graph pangenome captures missing heritability and empowers tomato breeding
https://www.nature.com/articles/s41586-022-04808-9#MOESM8
没有找到论文里的作图的代码,但是找到了部分组图数据,我们可以用论文中提供的原始数据模仿出论文中的图
今天的推文重复一下论文中的 Extended Data Fig7b
Extended Data Fig7c
箱线图和堆积柱形图
箱线图下方的8个矩形可以用拼图的方式来实现
箱线图的部分示例数据
image.png这里并没有提供箱线图的分组数据,这里我自己随便构造一列分组数据了,所以最终结果可能和原图不一致
整理数据
library(readxl)
dat01<-read_excel("data/20220711/41586_2022_4808_MOESM10_ESM.xlsx",
sheet = "Extend Fig7b",
skip = 1)
head(dat01)
library(tidyverse)
dat01 %>%
mutate(group_info=sample(c(rep("A",313),
rep("B",5),
rep("C",8),
rep("D",6)),
332,
replace = FALSE)) -> efig7c
箱线图代码
library(ggplot2)
library(latex2exp)
help(package="latex2exp")
dat<-data.frame(x=c(0.5,1:4),
y=-Inf,
label=c("n=",313,5,8,5))
ggplot(data=efig7c,
aes(x=group_info,y=BLUP))+
geom_boxplot(aes(fill=group_info))+
scale_fill_manual(values = c("#feb2a9","#fdd79d",
"#dbcde4","#c993c7"))+
geom_jitter(width = 0.4)+
theme_bw()+
theme(panel.grid = element_blank())+
annotate(geom = "text",
x=4,y=Inf,
label=TeX(r"(Kruskal Wallis, \textit{P} = 5 \times 10${^-}$${^7}$)"),
vjust=1,hjust=1)+
geom_text(data=dat,aes(x=x,y=y,label=label),
inherit.aes = FALSE,
vjust=-0.8)+
ylim(-40,NA)+
labs(y="BLUP value of expression")+
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
legend.position = "none") -> p1
p1
image.png
箱线图下方的矩形点
dat.2<-data.frame(x=rep(LETTERS[1:4],2),
y=rep(c(1,2),each=4),
group=c("A","A","A","D","A","B","D","A"))
dat.2
ggplot(data=dat.2,aes(x=x,y=y,fill=group,color=group))+
geom_point(shape=22,size=5)+
scale_fill_manual(values = c("#fc8072","#a1d99b",
"#4192c6"),
labels=c("Reference homozygous",
"Heterozygous",
"Alternate homozygous"))+
scale_color_manual(values = c("#fc8072","#a1d99b",
"#4192c6"),
labels=c("Reference homozygous",
"Heterozygous",
"Alternate homozygous"))+
theme_void() +
theme(legend.title = element_blank())+
annotate(geom = "text",
x=1,y=2,label="SV3_42936717",
hjust=1.2,size=3,vjust=1)+
annotate(geom = "text",
x=1,y=1,label="SV3_42954617",
hjust=1.2,size=3,vjust=0) -> p2
p2
image.png
将两个图组合到一起
library(ggpubr)
as_ggplot(get_legend(p2))
library(patchwork)
p1+
annotation_custom(grob = get_legend(p2),
xmin=3.5,xmax=3.5,ymin=-28,ymax=-28)+
p2+
theme(legend.position ="none")+
plot_layout(ncol = 1,heights = c(10,1)) -> p3
p3
image.png
最后是堆积柱形图的代码
数据集
image.pngdat02<-read_excel("data/20220711/41586_2022_4808_MOESM10_ESM.xlsx",
sheet = "Extend Fig7c")
dat02
dat02$x<-factor(dat02$x,
levels = c("SNPs","Indels","SVs"))
dat02
dat02 %>%
group_by(x) %>%
mutate(new_col=cumsum(y)) -> dat02
ggplot(data=dat02,aes(x=x,y=y,fill=group))+
geom_bar(stat="identity",
position = "stack")+
scale_fill_manual(values = c("Non-module"="#e99e9c",
"Module"="#98c0d7"))+
geom_text(aes(x=x,y=new_col,label=y),
vjust=1)+
labs(x=NULL,y=TeX(r"(\textit{h}${^2}$)"))+
theme_classic()+
scale_y_continuous(expand = expansion(mult = c(0,0)))+
theme(legend.position = c(0.2,0.8),
legend.title = element_blank(),
axis.title.y = element_text(angle = 0,vjust = 0.5)) -> p4
p4
image.png
最后是拼图
p3+p4
ggarrange(p3,p4,ncol = 2)
image.png
示例数据和代码可以自己到论文中获取,或者给本篇推文点赞,点击在看,然后留言获取
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!