最近在看論文 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這個地方好奇怪,遇到了幾個問題:
第三個小圖和第四。五個顏色和透明度都是設置一樣的,最後效果看起來 為 啥差別這麼大呢?沒有想明白原因
使用expression(italic("M. sieversii"))將標籤的字體設置為斜體的時候遇到警告信息
Warning messages:
1: In is.na(x) :
is.na() applied to non-(list or vector) of type 'expression'不知道是什麼原因!
論文中的圖折線看起來好像是平滑的,ggplot2畫折線圖的時候有沒有辦法能夠讓線變成平滑的呢?自己也查了資料,暫時也沒有找到辦法?歡迎大家留言討論以上的三個問題呀!
歡迎大家關注我的公眾號
小明的數據分析筆記本
公眾號二維碼.jpg