跟著Nature microbiology學(xué)作圖:R語言ggplot2做簇狀柱形圖并在坐標(biāo)軸添加分組標(biāo)記

論文

Evolutionary origins of the SARS-CoV-2 sarbecovirus lineage responsible for the COVID-19 pandemic

image.png

本地文件 s41564-020-0771-4.pdf

代碼和數(shù)據(jù)下載鏈接

https://github.com/plemey/SARSCoV2origins

今天的推文我們來重復(fù)一下論文中的 Figure 2 中的簇狀柱形圖

image.png

之前的推文也介紹過R語言ggplot2做簇狀柱形圖的代碼,這個圖和之前的比較常規(guī)的簇狀柱形圖的區(qū)別在于多了一個貼近坐標(biāo)軸的分組線段,今天的推文主要介紹的是這個的實現(xiàn)辦法

有一個R包是ggh4x,這里有函數(shù)可以直接控制坐標(biāo)軸的范圍

沒有找到原文的原始數(shù)據(jù),直接自己隨便構(gòu)造一個數(shù)據(jù)

image.png

加載需要用到的R包

library(readxl)
library(ggplot2)
library(ggh4x)

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


df<-read_excel("Figure2.xlsx")
df

最普通的簇狀柱形圖

ggplot(data=df,aes(x=x,y=y))+
  geom_col(aes(fill=group),
           position = position_dodge(0.4),
           width = 0.3)
image.png

設(shè)置柱子的先后順序

df$x<-factor(df$x,
             levels = c("NTD","CTD","Variable loop","S1 3 end","S2"))

df$group<-factor(df$group,
                 levels = c("Pangolin Guangdong 2019",
                            "Pangolin Guangxi 2017 (P5L)",
                            "Yunnan 2013 (RaTG13)"))

簡單美化

ggplot(data=df,aes(x=x,y=y))+
  geom_col(aes(fill=group),
           position = position_dodge(0.4),
           width = 0.3,
           show.legend = F)+
  scale_y_continuous(expand = c(0,0),
                     limits = c(-0.1,1))+
  theme_minimal()+
  theme(panel.grid = element_blank(),
        axis.line.y = element_line(),
        axis.ticks.y = element_line())+
  labs(x="loop removed",
       y="Genetic distance from human\nSARS-CoV-2\n(substitutions per site)")+
  annotate(geom = "segment",
           x=0.6,y=0,xend = 5.4,yend = 0)+
  scale_fill_manual(values = c("#ff7a17",
                               "#808284",
                               "#1db280"))+
  # theme(#plot.margin = unit(c(0.5,0.1,2,0.5),"cm"),
  #       
  #       axis.text.x = element_text(margin = 
  #                                    margin(2,0,0,0,unit = "cm")))+
  guides(y=guide_axis_truncated(trunc_lower = 0,
                                trunc_upper = 1)) -> p1
p1
image.png

最后是添加表示分組的柱子


 p1+ annotate(geom = "segment",
           x=0.6,xend=1.4,
           y=-0.05,yend = -0.05,
           size=5,
           color="#c2abd3")+
  annotate(geom = "segment",
           x=1.6,xend=2.4,
           y=-0.05,yend = -0.05,
           size=5,
           color="#d899be")+
  annotate(geom = "segment",
           x=2.6,xend=3.4,
           y=-0.05,yend = -0.05,
           size=5,
           color="#d5d6e9")+
  annotate(geom = "segment",
           x=3.6,xend=4.4,
           y=-0.05,yend = -0.05,
           size=5,
           color="#d899be")+
  annotate(geom = "segment",
           x=4.6,xend=5.4,
           y=-0.05,yend = -0.05,
           size=5,
           color="#f8a5c9")
image.png

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

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

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

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

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