跟着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
没有找到论文里的作图的代码,但是找到了部分做图数据,我们可以用论文中提供的原始数据模仿出论文中的图
今天的推文重复一下论文中的 Figure3a
Figure3b
Figure3c
频率分布直方图,堆积柱形图,散点图
image.png
频率分布直方图代码
library(readxl)
fig3a<-read_excel("data/20220711/41586_2022_4808_MOESM7_ESM.xlsx",
sheet = "Fig3a",
skip = 1)
head(fig3a)
dim(fig3a)
library(ggplot2)
library(latex2exp)
ggplot(data=fig3a,aes(x=h2))+
geom_histogram(aes(fill=type),
bins = 100)+
scale_fill_manual(values = c("#d1edcd","#94c2db","#fdbeb8"),
label=c(TeX(r"(\textit{h}${^2}$ (all variants) )"),
TeX(r"(\textit{h}${^2}$ (leading variants))"),
TeX(r"(\textit{h}${^2}$ (local variants) )")),
name="")+
theme_bw()+
theme(panel.grid = element_blank(),
legend.position = c(0.8,0.8))+
scale_x_continuous(expand = expansion(mult = c(0,0)),
breaks = seq(0,1,0.2))+
scale_y_continuous(expand = expansion(mult = c(0,0)))+
labs(y="Counts",
x=TeX(r"(\textit{h}${^2}$)"))+
geom_vline(xintercept = 0.27,lty="dashed",color="#94c2db")+
geom_vline(xintercept = 0.37,lty="dashed",color="#fdbeb8")+
geom_vline(xintercept = 0.62,lty="dashed",color="#d1edcd") -> p1
x<-c(0.27,0.37,0.62)
for (i in 1:3){
p1<-p1+
annotate(geom = "text",x=x[i],y=80,label=x[i],hjust=0)
}
p1
image.png
堆积柱形图
fig3b<-read_excel("data/20220711/41586_2022_4808_MOESM7_ESM.xlsx",
sheet = "Fig3b")
head(fig3b)
dim(fig3b)
fig3b$var2<-factor(fig3b$var2,
levels = c("MLM","LASSO","Overlapping"))
library(tidyverse)
fig3b %>%
group_by(var1) %>%
summarise(y=stack.bar.label.position(value),
y_label=value) %>%
ungroup() -> df.label
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)
}
ggplot(data=fig3b,aes(x=var1,y=value))+
geom_bar(stat="identity",
position = "stack",
aes(fill=var2))+
scale_fill_manual(values = c("#5ba555","#2baae1","#c6dcf0"),
name="",
label=c("MLM unique (11)",
"LASSO unique (1,249)",
"Overlapping (538)"))+
theme_classic()+
theme(legend.position = c(0.8,0.8))+
geom_text(data=df.label,
aes(x=var1,y=y,label=y_label)) -> p2
p2
image.png
最后的散点图
fig3c<-read_excel("data/20220711/41586_2022_4808_MOESM7_ESM.xlsx",
sheet = "Fig3c",
skip = 1)
head(fig3c)
dim(fig3c)
ggplot(data=fig3c %>% filter(Type=="MLM"),
aes(x=pos,y=-log10(pvalue)))+
geom_point(aes(shape=Variant,color=Variant,size=Variant))+
scale_color_manual(values = c("#868686","#b8275a"))+
theme_classic()+
scale_x_continuous(labels = function(x)
{sprintf("%0.2f",x/1000000)})+
labs(x="Chr3 (Mb)",
y=TeX(r"(-log${_1}{_0}$$\left[$\textit{P}$\right]$)"))+
geom_hline(yintercept = 6,lty="dashed")+
ggtitle("MLM")+
theme(legend.position = "none")+
scale_y_continuous(limits = c(0,10),
breaks = c(0,5,10)) -> p3.1
ggplot(data=fig3c %>% filter(Type=="LASSO"),
aes(x=pos,y=-log10(pvalue)))+
geom_point(aes(shape=Variant,color=Variant),
size=3)+
scale_color_manual(values = c("#b8275a"))+
scale_shape_manual(values = 17)+
theme_classic()+
scale_x_continuous(labels = function(x)
{sprintf("%0.2f",x/1000000)},
limits = c(42.90*1000000,43*1000000))+
labs(x="Chr3 (Mb)",
y=TeX(r"(-log${_1}{_0}$$\left[$\textit{P}$\right]$)"))+
geom_hline(yintercept = 6,lty="dashed")+
ggtitle("LASSO")+
theme(legend.position = "none")+
scale_y_continuous(breaks = c(0,5,10))+
geom_text(aes(label=ID),hjust=1.2) -> p3.2
library(patchwork)
p3.1/p3.2
image.png
最终的拼图
p1+p2 + (p3.1/p3.2)
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