1.箱線圖 Box plots
1.1 語(yǔ)法
geom_boxplot( mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2", ..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
stat_boxplot( mapping = NULL, data = NULL, geom = "boxplot", position = "dodge2", ..., coef = 1.5, na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
1.2 基礎(chǔ)箱形圖
# 將數(shù)據(jù)轉(zhuǎn)換為因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 繪制箱線圖
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()
p
# 旋轉(zhuǎn)箱形圖
p1 <- p + coord_flip()
# 缺口箱圖
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch=TRUE)
# 更改異常值,顏色,形狀和大小
p3 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(outlier.colour="red", outlier.shape=8,
outlier.size=4)
ggarrange(p,p1,p2,p3,nrow = 1)

# 帶有平均值點(diǎn)的箱形圖
p4 <- p + stat_summary(fun.y=mean, geom="point", shape=23, size=4)
p4
# 選擇要顯示的箱塊
p5 <- p + scale_x_discrete(limits=c("0.5", "2"))
p5
ggarrange(p4,p5)

1.3 帶點(diǎn)的箱形圖
# 帶點(diǎn)的箱形圖,可以使用函數(shù)geom_dotplot()或geom_jitter()將點(diǎn)添加到箱形圖中
# Box plot with dot plot
p6 <- p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
# Box plot with jittered points
# 0.2 : degree of jitter in x direction
p7 <- p + geom_jitter(shape=16, position=position_jitter(0.2))
ggarrange(p6,p7)

1.4 按組更改箱形圖顏色
# 按組更改箱形圖顏色
p8 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
geom_boxplot()
p8
# scale_color_manual:使用自定義顏色
p9 <- p8 + scale_color_manual(values=c("1", "2", "3"))
p9
# scale_color_brewer:使用RColorBrewer包中的調(diào)色板
p10 <- p8 + scale_color_brewer(palette="Dark2")
# scale_color_grey:使用灰色調(diào)色板
p11 <- p8 + scale_color_grey() + theme_classic()
ggarrange(p8,p9,p10,p11,nrow = 2,ncol = 2)

# 更改填充顏色
# Use single color
p12 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(fill='#A4A4A4', color="black")+
theme_classic()
# Change box plot colors by groups
p13 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p13
# 使用自定義調(diào)色板
p14 <- p13 +scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用brewer調(diào)色板
p15 <- p13 +scale_fill_brewer(palette="Dark2")
# 使用灰度
p16 <- p13 + scale_fill_grey() + theme_classic()
ggarrange(p13,p14,p15,p16,nrow = 1)

1.5 更改圖例中的項(xiàng)目順序
ToothGrowth$dose <- factor(ToothGrowth$dose,levels = c("2","1","0.5"))
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()

p17 <- p13 + scale_x_discrete(limits=c("2", "0.5", "1"))
p17

1.6 多組箱形圖
# 按組更改箱形圖顏色
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot()
# 改變位置
p18 <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(position=position_dodge(1))
p18
# 添加dot
p19 <- p18 + geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(1))
# 改變顏色
p20 <- p18 +scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
ggarrange(p18,p19,p20,nrow = 1)

1.7 定制箱形圖
# 基本箱形圖
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(fill="gray")+
labs(title="Plot of length per dose",x="Dose (mg)", y = "Length")+
theme_classic()
# 按組自動(dòng)更改顏色
bp <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()+
labs(title="Plot of length per dose",x="Dose (mg)", y = "Length")
bp1 <- bp + theme_classic()
# 連續(xù)色彩
bp2 <- bp + scale_fill_brewer(palette="Blues") + theme_classic()
# 離散顏色
bp3 <- bp + scale_fill_brewer(palette="Dark2") + theme_minimal()
# 漸變色
bp4 <- bp + scale_fill_brewer(palette="RdBu") + theme_minimal()
ggarrange(bp,bp1,bp2,bp3,bp4,nrow = 2,ncol = 3)

2.Violin plots
小提琴圖類(lèi)似于箱形圖,不同之處在于它們還顯示了不同值的數(shù)據(jù)的核概率密度。 通常,小提琴圖將包括數(shù)據(jù)中位數(shù)的標(biāo)記和指示四分位數(shù)范圍的框,如在標(biāo)準(zhǔn)框圖中一樣。
函數(shù)geom_violin()用于生成小提琴圖。
2.1 語(yǔ)法
geom_violin( mapping = NULL, data = NULL, stat = "ydensity", position = "dodge", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
stat_ydensity( mapping = NULL, data = NULL, geom = "violin", position = "dodge", ..., bw = "nrd0", adjust = 1, kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE, orientation = NA, show.legend = NA, inherit.aes = TRUE )
2.2 基本小提琴圖
# 將數(shù)值型變量轉(zhuǎn)換為因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 基本小提琴圖
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin()
p
# 旋轉(zhuǎn)
p1 <- p + coord_flip()
# 將trim參數(shù)設(shè)置為FALSE
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE)
# 選擇展示數(shù)據(jù)
p3 <- p + scale_x_discrete(limits=c("0.5", "2"))
# library(ggpubr)
ggarrange(p,p1,p2,p3,nrow = 1)

2.3 在小提琴圖上添加摘要統(tǒng)計(jì)信息
# 添加平均值
p4 <- p + stat_summary(fun.y=mean, geom="point", shape=23, size=2)
# 添加中位數(shù)
p5 <- p + stat_summary(fun.y=median, geom="point", size=2, color="red")
# 添加中位數(shù)和四分位數(shù),也即是箱線圖
p6 <- p + geom_boxplot(width=0.1)
# 添加平均值和標(biāo)準(zhǔn)偏差
p7 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE)
p8 <- p7 + stat_summary(fun.data="mean_sdl", mult=1,
geom="crossbar", width=0.2 )
p9 <- p7 + stat_summary(fun.data=mean_sdl, mult=1,
geom="pointrange", color="red")
ggarrange(p4,p5,p6,p7,p8,p9,nrow = 2,ncol = 3)

# 產(chǎn)生匯總統(tǒng)計(jì)的功能 (mean and +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p + stat_summary(fun.data=data_summary)

2.4 帶點(diǎn)小提琴圖
# 帶點(diǎn)小提琴圖geom_dotplot(),geom_jitter()
# violin plot with dot plot
p10 <- p7 + geom_dotplot(binaxis='y', stackdir='center', dotsize=1)
# violin plot with jittered points
# 0.2 : degree of jitter in x direction
p11 <- p7 + geom_jitter(shape=16, position=position_jitter(0.2))
ggarrange(p10,p11)

2.5 更改小提琴圖的顏色
# 按組更改小提琴圖的顏色
# 更改小提琴圖線顏色
# Change violin plot line colors by groups
p12 <-ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
geom_violin(trim=FALSE)
p12
# 使用自定義調(diào)色板
p13 <- p12 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用Brewer調(diào)色板
p14 <- p12 + scale_color_brewer(palette="Dark2")
# 使用灰度
p15 <- p12 + scale_color_grey() + theme_classic()
ggarrange(p12,p13,p14,p15,nrow = 1)

# 更改小提琴圖的填充顏色
# 單色
p16 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE, fill='#A4A4A4', color="darkred")+
geom_boxplot(width=0.1) + theme_minimal()
# 按組更改小提琴圖的顏色
p17 <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_violin(trim=FALSE)
ggarrange(p16,p17)

# 使用自定義調(diào)色板
p18 <- p17 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用Brewer調(diào)色板
p19 <- p17 + scale_fill_brewer(palette="Dark2")
# 使用灰度
p20 <- p17 + scale_fill_grey() + theme_classic()
ggarrange(p18,p17,p20,nrow = 1)

2.6 更改圖例位置
# 更改圖例位置
p21 <- p17 + theme(legend.position="top")
p22 <- p17 + theme(legend.position="bottom")
p23 <- p17 + theme(legend.position="none") # 移除圖例
ggarrange(p21,p22,p23,nrow = 1)

2.7 更改圖例中的項(xiàng)目順序
# 更改圖例中的項(xiàng)目順序
p24 <- p17 + scale_x_discrete(limits=c("2", "0.5", "1"))
ggarrange(p17,p24)

2.8 多組小提琴
## 多組小提琴
# 按組更改顏色
p25 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin()
# 改變位置
p26 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin(position=position_dodge(1))
# 添加點(diǎn)
p27 <- p26 + geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(1))
# 改變顏色
p28 <- p26 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
ggarrange(p25,p26,p27,p28,nrow = 1)

2.9 定制小提琴圖
## 定制小提琴圖
# 基礎(chǔ)小提琴圖
dp1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE, fill="gray")+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
geom_boxplot(width=0.1)+
theme_classic()
# 按組改變顏色
dp2 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_violin(trim=FALSE)+
geom_boxplot(width=0.1, fill="white")+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
dp3 <- dp + theme_classic()
# 連續(xù)型顏色
dp4 <- dp + scale_fill_brewer(palette="Blues") + theme_classic()
# D離散型顏色
dp5 <- dp + scale_fill_brewer(palette="Dark2") + theme_minimal()
# 漸變色
dp6 <- dp + scale_fill_brewer(palette="RdBu") + theme_minimal()
ggarrange(dp1,dp2,dp3,nrow = 1)
ggarrange(dp4,dp5,dp6,nrow = 1)


Reference
1.ggplot2 violin plot : Quick start guide - R software and data visualization
2.ggplot2 violin plot : Quick start guide - R software and data visualization