image.png
最近在看论文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication(高水平论文看起来还真是吃力!)看懂一点记一点吧。今天的笔记记录的是论文中Figure2图a的画法,图a展示的是啥内容我暂时还没有看懂,如果从画图的角度来说就是一个简单的折线图,正好之前有人问到如何添加灰色背景。今天先记录一下画图的内容
image.png
第一步模拟数据
从上至下的第一个
代码语言:javascript复制a<-seq(0,1.5,0.05)
df1<-data.frame(x=1:60,y=sample(a,60,replace=T))
画图
代码语言:javascript复制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复制b<-seq(0,2.5,0.05)
df3<-data.frame(x=1:60,y=sample(b,60,replace = T))
画图
代码语言:javascript复制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复制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复制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"))
将标签的字体设置为斜体的时候遇到警告信息
Warning messages:
1: In is.na(x) :
is.na() applied to non-(list or vector) of type 'expression'
不知道是什么原因!
- 第三个问题是: 论文中的图折线看起来好像是平滑的,ggplot2画折线图的时候有没有办法能够让线变成平滑的呢?自己也查了资料,暂时也没有找到办法?
欢迎大家留言讨论以上的三个问题呀!