跟着Nature学作图:R语言ggplot2箱线图和堆积柱形图完整示例
2023-02-19 12:27:44 时间
论文
Graph pangenome captures missing heritability and empowers tomato breeding
https://www.nature.com/articles/s41586-022-04808-9#MOESM8
s41586-022-04808-9.pdf
没有找到论文里的作图的代码,但是找到了部分做图数据,我们可以用论文中提供的原始数据模仿出论文中的图
今天的推文重复一下论文中的 Extended Data Fig. 4
箱线图和堆积柱形图
image.png
Extended Data Fig. 4a的部分示例数据截图
image.png
读取数据并作图
library(tidyverse)
extendedfig4a %>%
pivot_longer(-ID) %>%
mutate(group=name %>%
str_extract("linear|graph"),
x=name %>% str_replace("linear.|graph.","")) -> new.ef4a
new.ef4a$group<-factor(new.ef4a$group,
levels = c("linear","graph"))
new.ef4a$x<-factor(new.ef4a$x,
levels = c("snps","indels","svs",
"snps_indels","snps_indels_svs"))
library(latex2exp)
library(ggplot2)
ggplot(data=new.ef4a,
aes(x=x,y=value))+
geom_boxplot(aes(fill=group),
show.legend = FALSE)+
scale_fill_manual(values = c("#c0d5e5","#edd2c4"))+
scale_x_discrete(labels=c("SNPs","Indels","SVs",
"SNPs+Indels","SNPs+Indels+SVs"))+
labs(x=NULL,y=TeX(r"(\textit{h}${^2}$)"))+
theme_classic()+
theme(axis.title.y = element_text(angle=0,vjust=0.5))
image.png
Extended Data Fig. 4b的部分示例数据截图
image.png
读取数据并作图
extendedfig4b<-read_excel("data/20220711/41586_2022_4808_MOESM9_ESM.xlsx",
sheet = "Extend Fig4b",
skip = 1)
head(extendedfig4b)
extendedfig4b %>%
pivot_longer(-ID) %>%
mutate(group=name %>% str_extract("linear|graph"),
x=name %>% str_extract("overlapped|uniq")) -> new.ef4b
new.ef4b$group<-factor(new.ef4b$group,
levels = c("linear","graph"))
ggplot(data=new.ef4b,aes(x=x,y=value))+
geom_boxplot(aes(fill=group),key_glyphs="rect")+
scale_fill_manual(values = c("#c0d5e5","#edd2c4"),
labels=c("SL5.0-332","TGG1.1-332"))+
labs(x=NULL,y=TeX(r"(\textit{h}${^2}$)"))+
theme_classic()+
theme(axis.title.y = element_text(angle=0,vjust=0.5),
legend.position = "bottom",
legend.direction = "vertical",
legend.title = element_blank(),
legend.justification = c(0,0))
image.png
Extended Data Fig. 4c的部分示例数据截图
image.png
作图代码
extendedfig4c<-read_excel("data/20220711/41586_2022_4808_MOESM9_ESM.xlsx",
sheet = "Extend Fig4c")
extendedfig4c$group<-factor(extendedfig4c$group,
levels = c("SNPs","Indels","SVs"))
stack.bar.label.position<-function(x){
#x<-rev(x)
new.x<-vector()
for (i in 1:length(x)){
if (i == 1){
new.x<-append(new.x,x[i]/2)
}
else{
new.x<-append(new.x,sum(x[1:i-1])+x[i]/2)
}
}
return(new.x)
}
extendedfig4c %>%
group_by(x) %>%
summarise(y=stack.bar.label.position(value),
y_label=value) %>%
ungroup() -> df.label
df.label
ggplot(data=extendedfig4c,
aes(x=x,y=value))+
geom_bar(stat = "identity",
position = "stack",
aes(fill=group))+
scale_fill_manual(values = c("#8ea2cb",
"#a6d069",
"#ee8a6c"))+
geom_text(data=df.label,
aes(x=x,y=y,label=sprintf("%.2f",y_label)))+
labs(x=NULL,y=TeX(r"(\textit{h}${^2}$)"))+
theme_classic()+
scale_y_continuous(expand=expansion(mult = c(0,0)),
limits = c(0,0.5),
breaks = c(0,0.25,0.5))+
scale_x_discrete(labels=c("SL5.0-332","TGG1.1-332"))+
theme(legend.position = "top",
legend.title = element_blank(),
axis.title.y = element_text(angle=0,vjust=0.5))
image.png
最后是拼图
library(ggpubr)
ggarrange(ggarrange(p1,labels = "a"),
ggarrange(p2,p3,labels = c("b","c")),
ncol = 1)
library(patchwork)
p1/(p2+theme(legend.position = "top",
legend.direction = "horizontal")+p3)+
plot_annotation(tag_levels = "a")
image.png
示例数据和代码可以自己到论文中获取
相关文章
- Jgit的使用笔记
- 利用Github Action实现Tornadofx/JavaFx打包
- 叹息!GitHub Trending 即将成为历史!
- 微软软了?开源社区讨论炸锅,GitHub CEO 亲自来答
- GitHub Trending 列表频现重复项,前后端都没去重?
- Photoshop Elements 2021版本软件安装教程(mac+windows全版本都有)
- (ps全版本)Photoshop 2020的安装与破解教程(mac+windows全版本都有)
- (ps全版本)Photoshop cc2018的安装与破解教程(mac+windows全版本,包括2023
- 环境搭建:Oracle GoldenGate 大数据迁移到 Redshift/Flat file/Flume/Kafka测试流程
- 每个开发人员都要掌握的:最小 Linux 基础课
- 来撸羊毛了!Windows 环境下 Hexo 博客搭建,并部署到 GitHub Pages
- 超实用!手把手入门 MongoDB:这些坑点请一定远离
- 【GitHub日报】22-10-09 zustand、neovim、webtorrent、express 等4款App今日上新
- 【GitHub日报】22-10-10 brew、minio、vite、seaweedfs、dbeaver 等8款App今日上新
- 【GitHub日报】22-10-11 cobra、grafana、vue、ToolJet、redwood 等13款App今日上新
- Photoshop 2018 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2017 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2020 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2023 资源免费下载(mac+windows全版本都有,包括最新的2023)
- 最新版本Photoshop CC2018软件安装教程(mac+windows全版本都有,包括2023