R啟動時默認加載了boxplot()命令,可用於繪製箱線圖。我們使用boxplot()繪製箱線圖展示chao1指數在c分組中的分布,如下示例。
##boxplot() 箱線圖,詳情使用 ?boxplot 查看幫助
par(mfrow = c(1, 2))
#常規樣式
boxplot(value~group2, data = alpha3, col = '#f8766d', ylab = 'Chao1 (group c)')
#根據數據分布,添加凹槽
boxplot(value~group2, data = alpha3, col = '#f8766d', notch = TRUE, varwidth = TRUE, ylab = 'Chao1 (group c)')
ggplot2功能強大自然不必多說,以下繼續展示使用ggplot2繪製箱線圖的示例。
##ggplot2
library(ggplot2)
#單變量箱線圖
ggplot(alpha3, aes(x = group2, y = value)) +
geom_boxplot(outlier.size = 1, fill = '#f8766d') +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black')) +
labs(x = '', y = 'Chao1')
#將各數據值以抖動散點的方式添加在箱線圖中,同時繪製凹槽
ggplot(alpha3, aes(x = group2, y = value, fill = group1)) +
geom_boxplot(fill = '#f8766d', notch = TRUE) +
geom_jitter(color = 'red', show.legend = FALSE) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black')) +
labs(x = '', y = 'Chao1 (group c)')
#存在多分組時,多組分開展示的箱線圖
ggplot(alpha2, aes(x = group2, y = value, fill = group1)) +
geom_boxplot(outlier.size = 1) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'), legend.title = element_blank(), legend.key = element_blank()) +
labs(x = '', y = 'Chao1')
#多變量情況,添加分面的箱線圖
ggplot(alpha1, aes(x = group2, y = value, fill = group1)) +
geom_boxplot(outlier.size = 0.5, size = 0.5) +
facet_wrap(~variable, 2, scales = 'free') +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'), legend.title = element_blank(), legend.key = element_blank()) +
labs(x = '', y = 'Chao1')
#帶顯著性標記「*」的箱線圖
#先繪製箱線圖主體
p <- ggplot(data = alpha2, aes(x = group2, y = value, fill = group1)) +
geom_boxplot(outlier.size = 1) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'), legend.title = element_blank(), legend.key = element_blank()) +
labs(x = '', y = 'Chao1')
#再手動添加顯著性標記
#注意,這裡的顯著性是提前已經計算好的,我們通過手動輸入進來
#本篇只關注作圖,不涉及統計分析
library(doBy)
alpha2_stat <- summaryBy(value~group2, alpha2, FUN = max)
names(alpha2_stat) <- c('group2', 'value')
alpha2_stat$group1 <- NA
alpha2_stat$sig <- rep('***', 5)
p + geom_text(data = alpha2_stat, aes(label = sig), vjust = -0.3) +
annotate('text', x = alpha2_stat$group2, y = alpha2_stat$value, label = '———', vjust = -0.3)
#帶顯著性標記「abc」的箱線圖,
#先繪製箱線圖主體
p <- ggplot(data = alpha1, aes(x = group2, y = value, fill = group1)) +
geom_boxplot(outlier.size = 0.5, size = 0.5) +
facet_wrap(~variable, 2, scales = 'free') +
theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'), legend.title = element_blank(), legend.key = element_blank()) +
labs(x = '', y = 'Chao1')
#再手動添加顯著性標記
#同上所述,這裡的顯著性是提前通過差異分析已經計算好的,我們通過手動輸入進來
alpha1_stat <- summaryBy(value~group1+group2+variable, alpha1, FUN = max)
names(alpha1_stat) <- c('group1', 'group2', 'variable', 'value')
alpha1_stat$sig <- c('a', 'a', 'a', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'b', 'b', 'a', 'b', 'c', 'c', 'b', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'a')
p + geom_text(data = alpha1_stat, aes(label = sig, color = group1), position = position_dodge(1), vjust = -0.3)
好了,以上就是本篇的內容,大致簡介了幾種簡單的箱線圖在R中的繪製示例,以幫助剛接觸R的同學們入門,get到了嗎?