前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >跟着Nature Genetics学画图~ggplot2画折线图并在指定区域添加灰色背景

跟着Nature Genetics学画图~ggplot2画折线图并在指定区域添加灰色背景

作者头像
用户7010445
发布于 2021-01-20 07:46:28
发布于 2021-01-20 07:46:28
1.4K00
代码可运行
举报
运行总次数:0
代码可运行

image.png

最近在看论文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication(高水平论文看起来还真是吃力!)看懂一点记一点吧。今天的笔记记录的是论文中Figure2图a的画法,图a展示的是啥内容我暂时还没有看懂,如果从画图的角度来说就是一个简单的折线图,正好之前有人问到如何添加灰色背景。今天先记录一下画图的内容

image.png

第一步模拟数据

从上至下的第一个

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
a<-seq(0,1.5,0.05)
df1<-data.frame(x=1:60,y=sample(a,60,replace=T))

画图

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
library(ggplot2)
ggplot(df1,aes(x=x,y=y))+
  geom_line(size=1,color="#6994f3")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5))+
  labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label=expression(italic("M. sieversii")))

image.png

第二个和第一个一样,这里就不重复了,接下来是第三个,第三个多了一个灰色背景,这个可以借助geom_rect()函数实现

构造一份数据

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
b<-seq(0,2.5,0.05)
df3<-data.frame(x=1:60,y=sample(b,60,replace = T))

画图

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ggplot(df3,aes(x=x,y=y))+
  geom_rect(aes(xmin=5,xmax=12,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.1)+
  geom_rect(aes(xmin=23,xmax=28,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.1)+
  geom_rect(aes(xmin=35,xmax=52,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.1)+
  geom_rect(aes(xmin=55,xmax=59,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.1)+
  geom_line(size=1,color="#6994f3")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5),
        axis.text.x = element_blank())+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label="Gala")

image.png

接下来是最后一个,两条折线画到一起

这里采用的办法是两份数据集来叠加

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ggplot()+
  geom_rect(aes(xmin=5,xmax=12,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=23,xmax=28,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=35,xmax=52,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=55,xmax=59,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.)+
  geom_line(data=df5.1,aes(x=x,y=y),
            size=1,color="#80c97f")+
  geom_line(data=df5.2,aes(x=x,y=y),
            size=1,color="#a68dc8")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5))+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label="Gala Haplome B")

image.png

最后一步是将5个图拼接到一起
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
p1<-ggplot(df1,aes(x=x,y=y))+
  geom_line(size=1,color="#6994f3")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5))+
  labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label=expression(italic("M. sieversii")))

p2<-ggplot(df1,aes(x=x,y=y))+
  geom_line(size=1,color="#6994f3")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5))+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label=expression(italic("M. sylvestris")))
p3<-ggplot(df3,aes(x=x,y=y))+
  geom_rect(aes(xmin=5,xmax=12,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=23,xmax=28,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=35,xmax=52,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=55,xmax=59,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_line(size=1,color="#6994f3")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5),
        axis.text.x = element_blank())+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label="Gala")


p4<-ggplot()+
  geom_rect(aes(xmin=5,xmax=12,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=23,xmax=28,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=35,xmax=52,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=55,xmax=59,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.)+
  geom_line(data=df5.1,aes(x=x,y=y),
            size=1,color="#80c97f")+
  geom_line(data=df5.2,aes(x=x,y=y),
            size=1,color="#a68dc8")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5),
        axis.text.x = element_blank())+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label="Gala Haplome A")

p5<-ggplot()+
  geom_rect(aes(xmin=5,xmax=12,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=23,xmax=28,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=35,xmax=52,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.3)+
  geom_rect(aes(xmin=55,xmax=59,ymin=-Inf,ymax=Inf),
            fill="grey",alpha=0.)+
  geom_line(data=df5.1,aes(x=x,y=y),
            size=1,color="#80c97f")+
  geom_line(data=df5.2,aes(x=x,y=y),
            size=1,color="#a68dc8")+
  ylim(0,3)+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust=0.5))+
  #labs(title="Chr15")+
  annotate(geom = "text",x=5,y=2.8,
           label="Gala Haplome B")

library(cowplot)
pdf(file = "line_plot.pdf",height = 8,width = 6)
plot_grid(p1,p2,p3,p4,p5,
          ncol = 1,nrow=5)
dev.off()

image.png

这个地方好奇怪,遇到了几个问题:

  • 第一个问题是

第三个小图和第四。五个颜色和透明度都是设置一样的,最后效果看起来 为 啥差别这么大呢?没有想明白原因

  • 第二个问题是:

使用expression(italic("M. sieversii"))将标签的字体设置为斜体的时候遇到警告信息

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Warning messages:
1: In is.na(x) :
  is.na() applied to non-(list or vector) of type 'expression'

不知道是什么原因!

  • 第三个问题是: 论文中的图折线看起来好像是平滑的,ggplot2画折线图的时候有没有办法能够让线变成平滑的呢?自己也查了资料,暂时也没有找到办法?

欢迎大家留言讨论以上的三个问题呀!

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第一步模拟数据
  • 接下来是最后一个,两条折线画到一起
  • 最后一步是将5个图拼接到一起
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档