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
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'
不知道是什么原因!
欢迎大家留言讨论以上的三个问题呀!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有