跟著Nature學(xué)作圖:R語(yǔ)言ggplot2頻率分布直方圖

論文

Graph pangenome captures missing heritability and empowers tomato breeding

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

沒有找到論文里的作圖的代碼,但是找到了部分組圖數(shù)據(jù),我們可以用論文中提供的原始數(shù)據(jù)模仿出論文中的圖

今天的推文重復(fù)一下論文中的Figure2c 頻率分布直方圖

image.png

部分示例數(shù)據(jù)截圖

image.png

作圖數(shù)據(jù)用到的是R2那一列

讀取數(shù)據(jù)集

library(readxl)

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

這里第一行數(shù)據(jù)沒有用,我們可以選擇手動(dòng)刪除,或者設(shè)置讀取數(shù)據(jù)時(shí)不讀取第一行

作圖代碼

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

這里我個(gè)人認(rèn)為把直方圖的邊框加上然后顏色深一些可能會(huì)好看一點(diǎn)

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

這里新學(xué)到的知識(shí)點(diǎn):使用latex2exp這個(gè)R包的TeX()函數(shù)來(lái)添加文本比expression()函數(shù)好用

保留小數(shù)位數(shù)代碼 sprintf("%0.5f",0.12345678)

比如這里設(shè)置 橫坐標(biāo)軸標(biāo)題的斜體和上標(biāo)的代碼x=TeX(r"(\textit{R}$^2$)"

示例數(shù)據(jù)和代碼可以自己到論文中獲取,或者給本篇推文點(diǎn)贊,點(diǎn)擊在看,然后留言獲取

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容