日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

画箱线图_箱线图的N种画法

發布時間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 画箱线图_箱线图的N种画法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

圖中標示了箱線圖中每條線和點表示的含義,其中應用到了分位數的概念 線的主要包含五個數據節點,將一組數據從大到小排列,分別計算出他的上邊緣(Maximum),上四分位數(Q3),中位數(Median),下四分位數(Q1),下邊緣(Minimum) 不在上邊緣與下邊緣的范圍內的為異常值,用點表示。

數據準備

data <- data.frame(Value = rnorm(300),Repeat = rep(paste("Repeat", 1:3, sep = "_"), 100),Condition = rep(c("Control", "Test"), 150))> head(data)Value Repeat Condition 1 -1.1395507 Repeat_1 Control 2 0.7319707 Repeat_2 Test 3 -0.2219461 Repeat_3 Control 4 -1.1454664 Repeat_1 Test 5 1.0740937 Repeat_2 Control 6 0.3741845 Repeat_3 Test

boxplot函數(R自帶)

最方便的方法就是用boxplot函數,不需要依賴任何包

boxplot(data$Value, ylab="Value")

根據不同的條件,加上顏色

boxplot(Value ~ Condition, data=data, ylab="Value", col=c("darkred", "darkgreen")) boxplot(Value ~ Condition * Repeat, data=data, ylab="Value", col="darkgreen")

多個分組(condition和repeat)的箱線圖

boxplot(Value ~ Condition + Repeat, data=data, ylab="Value", col="darkgreen")

ggplot2

使用ggplot2來畫箱線圖是現在常用的方法

library(tidyverse)# 定義一種主題,方便后面重復使用 theme_boxplot <- theme(panel.background=element_rect(fill="white",colour="black",size=0.25),axis.line=element_line(colour="black",size=0.25),axis.title=element_text(size=13,face="plain",color="black"),axis.text = element_text(size=12,face="plain",color="black"),legend.position="none"# ggplot2畫圖 ggplot(data, aes(Condition, Value)) +geom_boxplot(aes(fill = Condition), notch = FALSE) +scale_fill_brewer(palette = "Set2") +theme_classic() + theme_boxplot

添加抖動散點

ggplot(data, aes(Condition, Value)) +geom_boxplot(aes(fill = Condition), notch = FALSE) +geom_jitter(binaxis = "y", position = position_jitter(0.2), stackdir = "center", dotsize = 0.4) +scale_fill_brewer(palette = "Set2") +theme_classic() + theme_boxplot

帶凹槽(notched)的箱線圖,中位數的置信區用凹槽表示

ggplot(data, aes(Condition, Value)) +geom_boxplot(aes(fill = Condition), notch = TRUE, varwidth = TRUE) +geom_jitter(binaxis = "y", position = position_jitter(0.2), stackdir = "center", dotsize = 0.4) +scale_fill_brewer(palette = "Set2") +theme_classic() + theme_boxplot

比較流行的小提琴圖,內嵌箱線圖和擾動散點

ggplot(data, aes(Condition, Value)) + geom_violin(aes(fill = Condition), trim = FALSE) +geom_boxplot(width = 0.2) +geom_jitter(binaxis = "y", position = position_jitter(0.2), stackdir = "center", dotsize = 0.4) +scale_fill_brewer(palette = "Set2") +theme_classic() + theme_boxplot

云雨圖,它是密度分布圖、箱線圖、散點圖的集合,完美的展示了所有數據信息

library(grid)# GeomFlatViolin函數的定義見https://github.com/EasyChart/Beautiful-Visualization-with-R ggplot(data, aes(Condition, Value, fill=Condition)) +geom_flat_violin(aes(fill = Condition), position = position_nudge(x=.25), color="black") +geom_jitter(aes(color = Condition), width=0.1) +geom_boxplot(width=.1, position=position_nudge(x=0.25), fill="white",size=0.5) +scale_fill_brewer(palette = "Set2") +coord_flip() + theme_bw() + theme_boxplot

分組畫箱線圖

根據不同的Condition和Repeat對數據分組畫圖

ggplot(data, aes(Repeat, Value)) +geom_boxplot(aes(fill = Condition), notch = FALSE, size = 0.4) +scale_fill_brewer(palette = "Set2") +guides(fill=guide_legend(title="Repeat")) +theme_bw()

同樣的,我們可以對箱線圖添加抖動點,但是分組之后,并不能直接添加抖動點,需要增加兩列信息來輔助畫抖動點

# 增加dist_cat和scat_adj ,用于畫抖動點 data <- data %>% mutate(dist_cat = as.numeric(Repeat),scat_adj = ifelse(Condition == "Control", -0.2, 0.2))# 增加之后的數據如下 > head(data)Value Repeat Condition dist_cat scat_adj 1 -1.1395507 Repeat_1 Control 1 -0.2 2 0.7319707 Repeat_2 Test 2 0.2 3 -0.2219461 Repeat_3 Control 3 -0.2 4 -1.1454664 Repeat_1 Test 1 0.2 5 1.0740937 Repeat_2 Control 2 -0.2 6 0.3741845 Repeat_3 Test 3 0.2ggplot(data, aes(Repeat, Value)) + geom_boxplot(aes(fill = Condition), notch = FALSE, size = 0.4) + geom_jitter(aes(scat_adj+dist_cat, Value, fill = factor(Condition)),position=position_jitter(width=0.1,height=0),alpha=1,shape=21, size = 1.2) +scale_fill_brewer(palette = "Set2") +guides(fill=guide_legend(title="Condition ")) +theme_bw()

小提琴圖本來是由兩個左右對稱的密度估計曲線構成,那么對數據分組之后,我們可以只保留兩個小提琴圖的各一半,這樣更能直接的觀察出兩組之間的差異!

# ggplot2并未提供這樣的功能,這里定義了geom_split_violin函數來實現 # geom_split_violin 的定義見 https://github.com/EasyChart/Beautiful-Visualization-with-R ggplot(data, aes(x = Repeat, y = Value, fill=Condition)) +geom_split_violin(draw_quantiles = 0.5, trim = FALSE) +geom_jitter(aes(scat_adj+dist_cat, Value, fill = factor(Condition)),position=position_jitter(width=0.1,height=0),alpha=1,shape=21, size = 1.2) +scale_fill_brewer(palette = "Set2") +guides(fill=guide_legend(title="Condition ")) +theme_bw()

ggpubr (帶顯著性的箱線圖)

生成數據

# 均值為3,標準差為1的正態分布 c1 <- rnorm(100, 3, 1) # Johnson分布的偏斜度2.2和峰度13 c2 <- rJohnson(100, findParams(3, 1, 2., 13.1)) # Johnson分布的偏斜度0和峰度20 c3 <- rJohnson(100, findParams(3, 1, 2.2, 20))data <- data.frame(Conditon = rep(c("C_1", "C_2", "C_3"), each = 100),Value = c(c1, c2, c3) )#數據如下 > head(data)Conditon Value 1 C_1 2.679169 2 C_1 1.699026 3 C_1 5.459568 4 C_1 3.778365 5 C_1 3.689881 6 C_1 1.295534

ggpubr的功能多樣,這里只舉箱線圖的例子

library(ggpubr) library(RColorBrewer)# 定義需要兩兩比較的組 compaired <- list(c("C_1", "C_2"), c("C_2", "C_3"), c("C_1", "C_3")) palette <- c(brewer.pal(7, "Set2")[c(1, 2, 4)])# wilcox.test ggboxplot(data, x = "Conditon", y = "Value",fill = "Conditon", palette = palette,add = "jitter", size=0.5) +stat_compare_means(comparisons = compaired, method = "wilcox.test") + # 添加每兩組變量的顯著性theme_classic() + theme_boxplot

使用ggplot2的語法添加顯著性檢驗,將wilcox.test 換成t.test

# t.test ggplot(data, aes(Conditon, Value))+geom_boxplot(aes(fill = Conditon), notch = FALSE, outlier.alpha = 1) +scale_fill_brewer(palette = "Set2") +geom_signif(comparisons = compaired,step_increase = 0.1,map_signif_level = F,test = t.test) +theme_classic() + theme_boxplot

歡迎關注公眾號:"生物信息學"

總結

以上是生活随笔為你收集整理的画箱线图_箱线图的N种画法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。