barplot()為R中繪製柱形圖的常用命令之一,R啟動時默認加載。以下是使用barplot()繪製物種堆疊柱形圖的一個簡單示例,用於展示細菌類群組成豐度。
#讀取數據
phylum_top10 <- read.csv('phylum_top10.csv', row.names = 1, stringsAsFactors = FALSE, check.names = FALSE)
#barplot() 作圖,可使用 ?barplot 查看該命令詳情
png('barplot_plot.png', width = 1000, height = 700)
par(xpd = TRUE, mar = par()$mar + c(1, 3, 1, 16))
barplot(as.matrix(100 * phylum_top10), col = c('#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3', '#FDB462', '#B3DE69', '#FCCDE5', '#BC80BD', '#CCEBC5', 'gray'),
legend = rownames(phylum_top10),
cex.axis = 2, cex.names = 2, ylim = c(0, 100), las = 1, width = 0.5, space = 0.5, beside = FALSE,
args.legend = list(x = 'right', bty = 'n', inset = -0.18, cex = 2, y.intersp = 1.2, x.intersp = 0.7, text.width = 1))
mtext('Relative Abundance(%)', cex = 2, side = 2, line = 4)
dev.off()
對於我個人來講,大多數時候更喜歡使用ggplot2。與barplot()相比,ggplot2可調節性強,功能更為強大,且通用性強。以下簡單展示ggplot2繪製物種堆疊柱形圖的方法。
library(reshape2)
library(ggplot2)
#整理成 ggplot2 作圖格式
phylum_top10$Taxonomy <- factor(rownames(phylum_top10), levels = rev(rownames(phylum_top10)))
phylum_top10 <- melt(phylum_top10, id = 'Taxonomy')
#添加分組,這次我們根據樣本分組繪製分面
group <- read.delim('group.txt', sep = '\t', stringsAsFactors = FALSE)
names(group)[1] <- 'variable'
phylum_top10 <- merge(phylum_top10, group, by = 'variable')
#繪製帶分面的柱狀圖
p <- ggplot(phylum_top10, aes(variable, 100 * value, fill = Taxonomy)) +
geom_col(position = 'stack', width = 0.6) +
facet_wrap(~group, scales = 'free_x', ncol = 2) +
scale_fill_manual(values = rev(c('#8DD3C7', '#FFFFB3', '#BEBADA', '#FB8072', '#80B1D3', '#FDB462', '#B3DE69', '#FCCDE5', '#BC80BD', '#CCEBC5', 'gray'))) +
labs(x = '', y = 'Relative Abundance(%)') +
theme(panel.grid = element_blank(), panel.background = element_rect(color = 'black', fill = 'transparent'), strip.text = element_text(size = 12)) +
theme(axis.text = element_text(size = 12), axis.title = element_text(size = 13), legend.title = element_blank(), legend.text = element_text(size = 11))
#ggsave('ggplot2_plot.pdf', p, width = 8, height = 6)
ggsave('ggplot2_plot.png', p, width = 8, height = 6)
補充內容,關於ggplot2的柱形圖參數,geom_col(position = 'stack')和geom_col(position = 'fill'),均可用於繪製堆疊柱形圖,但是這兩個參數的作圖結果是不一樣的。
#比方說我們剔除其中的「Others」,作為展示
phylum_top10_noothers <- subset(phylum_top10, Taxonomy != 'Others')
#參數 geom_col(position = 'stack')
ggplot(phylum_top10_noothers, aes(variable, value, fill = Taxonomy)) +
geom_col(position = 'stack', width = 0.6) +
labs(x = '', y = 'Relative Abundance')
#參數 geom_col(position = 'fill')
ggplot(phylum_top10_noothers, aes(variable, value, fill = Taxonomy)) +
geom_col(position = 'fill', width = 0.6) +
labs(x = '', y = 'Relative Abundance')
下圖A為參數「geom_col(position = 'stack')」的作圖結果,y軸展示原始計數,即作圖數據中給定值為多少即展示多少,y軸取值最大值為作圖數據中各樣本中細菌類群豐度總和;B為「position = 'fill'」,首先再次將作圖數據中每個類群的豐度除以其在各樣本中的總豐度,即展示重新計算的比例,y軸值最大值默認為1。
以上分別展示了barplot()以及ggplot2繪製物種堆疊柱形圖的2個簡單示例。本篇作為簡單介紹,更詳細更複雜的繪製方法還需大家在實踐中慢慢領悟了。