基因组数据绘图ggplot2绘图可视化分析

跟着Nature Genetics学画图~ggplot2画折线图

2021-01-17  本文已影响0人  小明的数据分析笔记本
image.png

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

image.png
第一步模拟数据

从上至下的第一个

a<-seq(0,1.5,0.05)
df1<-data.frame(x=1:60,y=sample(a,60,replace=T))

画图

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()函数实现

构造一份数据

b<-seq(0,2.5,0.05)
df3<-data.frame(x=1:60,y=sample(b,60,replace = T))

画图

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
接下来是最后一个,两条折线画到一起

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

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个图拼接到一起
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

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

Warning messages:
1: In is.na(x) :
  is.na() applied to non-(list or vector) of type 'expression'

不知道是什么原因!

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

欢迎大家关注我的公众号
小明的数据分析笔记本

上一篇下一篇

猜你喜欢

热点阅读