箱線圖(box plot)主要從四分位數(shù)的角度展示數(shù)據(jù)的分布,最大值(Q4)、上四分位(Q3)、中位數(shù)(Q2)、下四分位數(shù)(Q1)和最小值(Q0)5個位置來獲取一維數(shù)據(jù)的分布概況。
1、系統(tǒng)環(huán)境為Windows
官網(wǎng)下載安裝R語言和Rstudio
https://cloud.r-project.org/
https://github.com/rstudio/rstudio
2、安裝繪圖用的r 包ggplot2
設置工作目錄
setwd('I:/R_practice)
install.packages("ggplots")
加載包
3、導入數(shù)據(jù)
library(ggplot2)
library(readxl)
library(ggsignif)
tair_height_length <- read_excel("tair_height_length.xlsx") excel 表格在當前工作目錄
View(tair_height_length)

(I@DU0TD2)%~IOLP}3E{LWN.png
4、先用基礎函數(shù)boxplot(),繪制箱線圖
boxplot(height ~ type, data= tair_height_length)

image.png
5、再用ggplot2 繪制
p = ggplot(aes(x = height, y = type), data = tair_height_length) + geom_boxplot(outlier.shape = 20) + labs(x= "高度",y= "類型") + coord_flip()+geom_signif( # 添加顯著性標簽
#comparisons=list(c("VI","QF"),c("QF","QB"),c('VI','QF')), # 選擇你想在哪2組上添加標簽
comparisons=list(c("WT","IAA")),
step_increase = 0.1,
test="t.test", # "t 檢驗,比較兩組(參數(shù))" = "t.test","Wilcoxon 符號秩檢驗,比較兩組(非參數(shù))" = "wilcox.test"
map_signif_level=T # 標簽樣式F為數(shù)字,T為*號
)
print(p)
p = ggplot(aes(x = height, y = type), data = tair_height_length) + geom_boxplot(outlier.shape = 4) + labs(x= "高度",y= "類型") + coord_flip()
print(p)

image.png
6、對數(shù)據(jù)做方差分析
tair_height_length.aov = aov(height ~ type, data = tair_height_length)
summary(tair_height_length.aov)