跟着Nature学作图:R语言ggplot2频率分布直方图

论文

Graph pangenome captures missing heritability and empowers tomato breeding

https://www.nature.com/articles/s41586-022-04808-9#MOESM8

没有找到论文里的作图的代码,但是找到了部分组图数据,我们可以用论文中提供的原始数据模仿出论文中的图

今天的推文重复一下论文中的Figure2c 频率分布直方图

image.png

部分示例数据截图

image.png

作图数据用到的是R2那一列

读取数据集

library(readxl)

dat.fig2c<-read_excel("data/20220711/41586_2022_4808_MOESM6_ESM.xlsx",
                      sheet = "Fig2c",
                      skip = 1)
head(dat.fig2c)

这里第一行数据没有用,我们可以选择手动删除,或者设置读取数据时不读取第一行

作图代码

library(ggplot2)
library(latex2exp)

ggplot(data=dat.fig2c,aes(x=R2))+
  geom_histogram(aes(y=after_stat(count / sum(count)),
                     fill=Type),
                 bins = 150,
                 alpha=0.3)+
  scale_fill_manual(values = c("InDel-SV"="#a3cd5b",
                               "SNP-SV"="#8ea0cc"),
                    labels=c("InDel-SV"="InDel versus SV",
                             "SNP-SV"="SNP versus SV"))+
  theme_bw()+
  theme(panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line = element_line(),
        legend.position = c(0.1,0.9),
        legend.direction = "horizontal",
        legend.background = element_rect(fill="transparent"),
        legend.title = element_blank(),
        legend.justification = c(0,1))+
  scale_x_continuous(limits = c(0,1),
                     expand = expansion(mult = c(0,0)))+
  scale_y_continuous(limits = c(0,0.025),
                     expand = expansion(mult = c(0,0)),
                     breaks = seq(0,0.025,0.005),
                     labels = function(x){sprintf("%0.1f",x*100)})+
  labs(x=TeX(r"(\textit{R}$^2$)"),
       y="Frequency (%)")+
  geom_vline(xintercept = 0.7,lty="dashed") -> p1

p1

image.png

这里我个人认为把直方图的边框加上然后颜色深一些可能会好看一点

ggplot(data=dat.fig2c,aes(x=R2))+
  geom_histogram(aes(y=after_stat(count / sum(count)),
                     fill=Type),
                 bins = 150,
                 alpha=1,
                 color="black")+
  scale_fill_manual(values = c("InDel-SV"="#a3cd5b",
                               "SNP-SV"="#8ea0cc"),
                    labels=c("InDel-SV"="InDel versus SV",
                             "SNP-SV"="SNP versus SV"))+
  theme_bw()+
  theme(panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line = element_line(),
        legend.position = c(0.1,0.9),
        legend.direction = "horizontal",
        legend.background = element_rect(fill="transparent"),
        legend.title = element_blank(),
        legend.justification = c(0,1))+
  scale_x_continuous(limits = c(0,1),
                     expand = expansion(mult = c(0,0)))+
  scale_y_continuous(limits = c(0,0.025),
                     expand = expansion(mult = c(0,0)),
                     breaks = seq(0,0.025,0.005),
                     labels = function(x){sprintf("%0.1f",x*100)})+
  labs(x=TeX(r"(\textit{R}$^2$)"),
       y="Frequency (%)")+
  geom_vline(xintercept = 0.7,lty="dashed") -> p2

p2
image.png

拼图

library(patchwork)
p1+p2
image.png

这里新学到的知识点:使用latex2exp这个R包的TeX()函数来添加文本比expression()函数好用

保留小数位数代码 sprintf("%0.5f",0.12345678)

比如这里设置 横坐标轴标题的斜体和上标的代码x=TeX(r"(\textit{R}$^2$)"

示例数据和代码可以自己到论文中获取,或者给本篇推文点赞,点击在看,然后留言获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

你可能感兴趣的:(跟着Nature学作图:R语言ggplot2频率分布直方图)