前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Plants学作图:R语言ggplot2画分组折线图并对坐标轴添加一些额外注释

跟着Nature Plants学作图:R语言ggplot2画分组折线图并对坐标轴添加一些额外注释

作者头像
用户7010445
发布2023-01-06 18:31:41
8910
发布2023-01-06 18:31:41
举报

论文

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

读取数据集

代码语言:javascript
复制
library(readxl)

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

指定列按照行来求平均值

代码语言:javascript
复制
library(tidyverse)

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

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

代码语言:javascript
复制
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"))

基本的分组折线图和添加文本

代码语言:javascript
复制
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

在坐标轴区域添加注释

并对主题进行一些修改

代码语言:javascript
复制
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

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

示例数据可以到论文中去下载,代码可以在推文中复制

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 数据下载链接
  • 部分示例数据集
  • 读取数据集
  • 指定列按照行来求平均值
  • 新构造一些数据用来添加绘图区域内的文本
  • 基本的分组折线图和添加文本
  • 在坐标轴区域添加注释
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档