跟著Nature Plants學(xué)作圖:R語(yǔ)言ggplot2畫(huà)分組折線圖展示甲基化水平

論文

The flying spider-monkey tree fern genome provides insights into fern evolution and arborescence

https://www.nature.com/articles/s41477-022-01146-6#Sec44

數(shù)據(jù)下載鏈接

https://doi.org/10.6084/m9.figshare.19125641

今天的推文重復(fù)一下論文中的Figure1b左上角的小圖

image.png

今天推文的主要知識(shí)點(diǎn)是如何在繪圖區(qū)域外添加一些文本和線段的注釋,這里需要用到annotation_custom()函數(shù)

部分示例數(shù)據(jù)集

image.png

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

library(readxl)

dat01<-read_excel("data/20220524/Nature_plant_fig1b.xlsx")
head(dat01)

指定列按照行來(lái)求平均值

library(tidyverse)

dat01 %>% 
  mutate(new_col=rowMeans(.[,4:6])) -> new.dat

新構(gòu)造一些數(shù)據(jù)用來(lái)添加繪圖區(qū)域內(nèi)的文本

dftext<-data.frame(x=c(150,150,150,112.5),
                   y=c(15,70,95,20)/100,
                   label=c("0.03","66.83","88.97","Centromere"))

基本的分組折線圖和添加文本

library(ggplot2)
ggplot()+
  geom_line(data=new.dat,aes(x=Window,y=new_col,color=Context),
            size=2)+
  geom_vline(xintercept = 100,lty="dashed",
             color="red",
             size=1)+
  geom_vline(xintercept = 125,lty="dashed",
             color="red",
             size=1)+
  geom_text(data=dftext,aes(x=x,y=y,label=label))
image.png

在坐標(biāo)軸區(qū)域添加注釋

并對(duì)主題進(jìn)行一些修改

library(grid)
ggplot()+
  geom_line(data=new.dat,aes(x=Window,y=new_col,color=Context),
            size=2)+
  geom_vline(xintercept = 100,lty="dashed",
             color="red",
             size=1)+
  geom_vline(xintercept = 125,lty="dashed",
             color="red",
             size=1)+
  geom_text(data=dftext,aes(x=x,y=y,label=label))+
  coord_cartesian(clip = "off")+
  annotation_custom(grob = linesGrob(gp=gpar(col="#e9d3ff",
                                             lwd=8,
                                             lineend="square")),
                    xmin=0,xmax=100,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = linesGrob(gp=gpar(col="#e9d3ff",
                                             lwd=8,
                                             lineend="square")),
                    xmin=125,xmax=225,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = linesGrob(gp=gpar(col="#f7931e",
                                             lwd=8,
                                             lineend="square")),
                    xmin=0,xmax=1,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = linesGrob(gp=gpar(col="#93278f",
                                             lwd=8,
                                             lineend="square")),
                    xmin=99,xmax=100,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = linesGrob(gp=gpar(col="#93278f",
                                             lwd=8,
                                             lineend="square")),
                    xmin=124,xmax=125,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = linesGrob(gp=gpar(col="#f7931e",
                                             lwd=8,
                                             lineend="square")),
                    xmin=224,xmax=225,
                    ymin=-0.08,
                    ymax=-0.08)+
  annotation_custom(grob = pointsGrob(pch=19,
                                      size=unit(0.8,'char')),
                    xmin=112.5,ymin=-0.08,
                    xmax=112.5,ymax=-0.08)+
  annotation_custom(grob = segmentsGrob(gp=gpar(col="black",
                                             lwd=2,
                                             lineend="square"),
                                        arrow = arrow()),
                    xmin=112.5,
                    ymin=0.125,
                    xmax=112.5,
                    ymax=-0.02)+
  annotation_custom(grob = segmentsGrob(gp=gpar(col="black",
                                                lwd=2,
                                                lineend="square"),
                                        arrow = arrow(angle=20,
                                                      length = unit(2,'mm'))),
                    xmin=100,
                    ymin=-0.2,
                    xmax=100,
                    ymax=-0.08)+
  annotation_custom(grob = segmentsGrob(gp=gpar(col="black",
                                                lwd=2,
                                                lineend="square"),
                                        arrow = arrow(angle=20,
                                                      length = unit(2,'mm'))),
                    xmin=125,
                    ymin=-0.2,
                    xmax=125,
                    ymax=-0.08)+
  annotation_custom(grob = textGrob(label = "Pericentromeric regions",
                                    just = "top",
                                    hjust=0.5),
                    xmin=112.5,
                    ymin=-0.2,
                    xmax=112.5,
                    ymax=-0.2)+
  annotation_custom(grob = segmentsGrob(gp=gpar(col="black",
                                                lwd=2,
                                                lineend="square"),
                                        arrow = arrow(angle=20,
                                                      length = unit(2,'mm'))),
                    xmin=225,
                    ymin=-0.2,
                    xmax=225,
                    ymax=-0.08)+
  annotation_custom(grob = textGrob(label = "Teloere",
                                    just = "top",
                                    hjust=0.5),
                    xmin=225,
                    ymin=-0.2,
                    xmax=225,
                    ymax=-0.2)+
  scale_y_continuous(limits = c(0,1),
                     breaks = seq(0,1,0.25),
                     labels = seq(0,1,0.25)*100)+
  scale_color_manual(values = c("#66c2a5","#fc8d62","#8da0cb"))+
  labs(x="",y="Methylation level (%)")+
  theme_bw()+
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        plot.margin = unit(c(0.1,0.1,2,0.1),'cm'),
        panel.grid = element_blank())

最終結(jié)果如下

image.png

說(shuō)實(shí)話,額外注釋的位置真不好搞,這些額外注釋可能還是出圖后借助其他軟件來(lái)編輯比較合適

示例數(shù)據(jù)可以到論文中去下載,代碼可以在推文中復(fù)制,或者給推文打賞一元獲取我整理好的數(shù)據(jù)和代碼

歡迎大家關(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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