【跟著CNS來作圖1】堆積柱狀圖的多層注釋

后面我們會(huì)陸陸續(xù)續(xù)有個(gè)系列,跟著別人好的paper中的圖進(jìn)行學(xué)習(xí),試著重復(fù)別人圖中的效果,來加強(qiáng)自己的學(xué)習(xí)。

今天就來測試和學(xué)習(xí)下面這個(gè)paper中的一個(gè)堆積柱狀圖,主要是分層注釋的練習(xí)。

因?yàn)椋瑳]有paper中的數(shù)據(jù),我們做測試就隨機(jī)生成一點(diǎn)數(shù)據(jù)。

先分解整個(gè)圖:

1. 堆積柱狀圖

2. 2層注釋:上面一層注釋,沒3個(gè)一組注釋,代表取樣部位。下面注釋代表取樣時(shí)期。

3. 3層注釋,按樣本的宏基因組來源分類。

4. 第4層,3個(gè)文本信息


所以先生成一個(gè)測試數(shù)據(jù),來表征柱狀圖,從圖中看,包含21組樣本,每個(gè)樣本里面5種不同的變量。

library(reshape2)

library(ggnewscale)

library(ggplot2)

library(tidyverse)

#所以我們生成一個(gè)21行,5列的矩陣,5列的名稱與paper中一樣。

df <- as.data.frame(matrix(data = sample(0:50,105,replace = T),ncol = 5))

colnames(df) <- c('Heterogeneous selection','Homogeneous selection',

? ? ? ? ? ? ? ? ? 'Dispersal limitation','Homogenizing dispersal',

? ? ? ? ? ? ? ? ? 'Undominated')

#因?yàn)閳D中畫的是百分比,我們轉(zhuǎn)化成百分比信息

back.data<- data.frame()

for(n in 1:nrow(df))

{

? tmp <- df[n,]/sum(df[n,])

? back.data <- rbind(back.data,tmp)

}

back.data$ID <- rownames(back.data)

df_plot <- melt(back.data,id = "ID")? ? ? ? #轉(zhuǎn)化成短矩陣

df_plot$ID <- factor(df_plot$ID,levels=seq(1:27))? ? ? ??

col <- c("purple","green","gold","red","gray")

#下面我們先畫出基本的堆積柱狀圖框架

p <- ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))

p

下面,我們就要開始添加注釋信息,信息我們以矩陣框的形式來添加,前面的散點(diǎn)圖和曼哈頓圖的例子我們也介紹過這種用法:

geom_rect()和geom_tile()函數(shù)的功能是一致的,但是參數(shù)有所區(qū)別:geom_rect()使用的是矩形四個(gè)頂點(diǎn)的位置,即xmin、xmax、ymin和ymax,而geom_tile()使用的是矩形的中心位置及其尺寸,即x、y、width、height。geom_tile()是geom_tile()的特例,其要求所有矩形的尺寸相同。

我們今天使用geom_rect來實(shí)現(xiàn)。

第一層的注釋信息,應(yīng)該包含下面的樣本:

我們先生成一個(gè)矩形框的注釋文件,文件包含注釋框的ID,以及開始和結(jié)束位置:

annotation1 <- data.frame(ID=c("Plastic leaf","Phylloplane","Leaf endosphere","Rhizoplane","Root endosphere","Rhizophere soil","Bulk soil"),

? ? ? ? ? ? ? ? ? ? ? ? ? xmin=seq(1,19,3),xmax=seq(3,21,3))

annotation1$ID <- factor(annotation1$ID,levels = annotation1$ID)

#然后根據(jù)注釋信息添加矩形框

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')

從圖中可以發(fā)現(xiàn),對于注釋框的其實(shí)和結(jié)束位置,我們需要在中心位置往前往后添加一半的距離,但是因?yàn)椴煌M之間需要有縫隙,所以我們就添加0.45的距離。

annotation1 <- data.frame(ID=c("Plastic leaf","Phylloplane","Leaf endosphere","Rhizoplane","Root endosphere","Rhizophere soil","Bulk soil"),xmin=seq(1,19,3)-0.45,xmax=seq(3,21,3)+0.45)

annotation1$ID <- factor(annotation1$ID,levels = annotation1$ID)

#在原先其實(shí)距離的基礎(chǔ)上,望前望后添加了0.45的距離,因?yàn)樘砑?.5,組和組之間就沒有縫隙了。

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')

下面添加第二層的注釋信息,第二層相當(dāng)于有27個(gè)注釋,顏色按發(fā)育期來添色。所以,我們先添加一樣的27個(gè)框,顏色按生育期著色,組之間的位置我們再調(diào)整。

annotation2 <- data.frame(ID=rep(c("Seedling stage","Tasseling stage","Mature stage"),7),

? ? ? ? ? ? ? ? ? ? ? ? ? xmin=seq(0.5,20.5,1),xmax=seq(1.5,21.5,1))

annotation2$ID <- factor(annotation2$ID,levels = c("Seedling stage","Tasseling stage","Mature stage"))

#上面構(gòu)建了一個(gè)關(guān)于二層注釋框的位置信息,和生育期信息。

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_brewer(palette = 'Set1',name="Stage")

但是,因?yàn)橛?個(gè)組,所以組和組之間是需要留點(diǎn)空隙的,這樣子,我們就需要對比如3-4,6-7,9-10,12-13,15-16,18-19之間需要微調(diào),各自減去0.05。而1和21也各自減去0.05。

這樣來說,需要加上0.05的有1,4,7,10,13,16,19。而需要往減去0.05的有3,6,9,12,15,18,21

annotation2 <- data.frame(ID=rep(c("Seedling stage","Tasseling stage","Mature stage"),7),

? ? ? ? ? ? ? ? ? ? ? ? ? xmin=seq(0.5,20.5,1),xmax=seq(1.5,21.5,1))

annotation2$ID <- factor(annotation2$ID,levels = c("Seedling stage","Tasseling stage","Mature stage"))

index1 <- seq(1,19,3)

annotation2$xmin[index1] <- annotation2$xmin[index1] +0.05

index2 <- seq(3,21,3)

annotation2$xmax[index2] <- annotation2$xmax[index2] -0.05

這樣操作之后,獲得我們新的第二層注釋文件。

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_brewer(palette = 'Set1',name="Stage")

這樣畫出來的圖,在第二層注釋之間就會(huì)有間隙了。

第三層比較簡單,就是三組。

annotation3 <- data.frame(ID=c("Air","Plant","Soil"),

? ? ? ? ? ? ? ? ? ? ? ? ? xmin=c(1,4,16)-0.45,xmax=c(3,15,21)+0.45)

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_brewer(palette = 'Set1',name="Stage")+

new_scale('fill') +

geom_rect(data=annotation3,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.13,ymax = -0.12),fill="black",

? ? ? ? ? show.legend = F)

第4層就是三個(gè)文本信息,位置其實(shí)就是第3層的中間位置。

所以基本就是x軸的2,9.5,18.5

所以我們添加文本信息:

ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_brewer(palette = 'Set1',name="Stage")+

new_scale('fill') +

geom_rect(data=annotation3,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.13,ymax = -0.12),fill="black",

? ? ? ? ? show.legend = F)+

geom_text(aes(x = 2,y = -0.18,label = 'Air'),size = 8) +

geom_text(aes(x = 9.5,y = -0.18,label = 'Plant'),size = 8) +

geom_text(aes(x = 18.5,y = -0.18,label = 'Soil'),size = 8)

下面,我們來調(diào)整一下legend的位置,用cowplot::plot_grid來實(shí)現(xiàn)。

main_plot <- ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill())+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID),show.legend = F)+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID),show.legend = F)+

scale_fill_brewer(palette = 'Set1',name="Stage")+

new_scale('fill') +

geom_rect(data=annotation3,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.13,ymax = -0.12),fill="black",

? ? ? ? ? show.legend = F)+

geom_text(aes(x = 2,y = -0.18,label = 'Air'),size = 8) +

geom_text(aes(x = 9.5,y = -0.18,label = 'Plant'),size = 8) +

geom_text(aes(x = 18.5,y = -0.18,label = 'Soil'),size = 8)


lg1 <- ggplot(df_plot)+

geom_col(aes( x = ID, y = value, fill = variable), position = position_fill(),show.legend = F)+

scale_fill_manual(values = col,name="Ecological process")+

xlab(NULL)+

ylab("Relative Importantce")+

scale_y_continuous(labels = scales::percent_format())+

theme(axis.text.x = element_blank(),

? ? ? axis.ticks.x=element_blank(),

? ? ? text = element_text(size = 15))+

new_scale('fill') +

geom_rect(data=annotation1,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.06,ymax = -0.02,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_manual(values = c('#FA26A0','#388E3C','#77ACF1',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '#FFC947','#B85C38','#AAAAAA','#364547'),

? ? ? ? ? ? ? ? ? name = 'Niche')+

new_scale('fill') +

geom_rect(data=annotation2,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.11,ymax = -0.07,

? ? ? ? ? ? ? ? ? ? ? ? ? ? fill = ID))+

scale_fill_brewer(palette = 'Set1',name="Stage")+

new_scale('fill') +

geom_rect(data=annotation3,aes(xmin = xmin,xmax = xmax,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ymin = -0.13,ymax = -0.12),fill="black",

? ? ? ? ? show.legend = F)+

geom_text(aes(x = 2,y = -0.18,label = 'Air'),size = 8) +

geom_text(aes(x = 9.5,y = -0.18,label = 'Plant'),size = 8) +

geom_text(aes(x = 18.5,y = -0.18,label = 'Soil'),size = 8) +

theme(legend.direction = "horizontal", legend.position = "bottom")


niche_legend <- cowplot::get_legend(lg1)? ?#獲取采樣部位和時(shí)期的legend信息


cowplot::plot_grid(plotlist = list(main_plot,niche_legend),ncol = 1, nrow = 2,

? ? ? ? ? rel_heights = c(5, 1))

多個(gè)圖的話,可以一起合。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容