跟着Nature Plants学作图:R语言ggplot2画分组折线图展示甲基化水平

论文

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

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

数据下载链接

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

今天的推文重复一下论文中的Figure1b左上角的小图

image.png

今天推文的主要知识点是如何在绘图区域外添加一些文本和线段的注释,这里需要用到annotation_custom()函数

部分示例数据集

image.png

读取数据集

library(readxl)

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

指定列按照行来求平均值

library(tidyverse)

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

新构造一些数据用来添加绘图区域内的文本

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

在坐标轴区域添加注释

并对主题进行一些修改

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())

最终结果如下

image.png

说实话,额外注释的位置真不好搞,这些额外注释可能还是出图后借助其他软件来编辑比较合适

示例数据可以到论文中去下载,代码可以在推文中复制,或者给推文打赏一元获取我整理好的数据和代码

欢迎大家关注我的公众号

小明的数据分析笔记本

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

你可能感兴趣的:(跟着Nature Plants学作图:R语言ggplot2画分组折线图展示甲基化水平)