1.箱线图 Box plots
1.1 语法
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 基础箱形图
# 将数据转换为因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 绘制箱线图
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()
p
# 旋转箱形图
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)

# 带有平均值点的箱形图
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 带点的箱形图
# 带点的箱形图,可以使用函数geom_dotplot()或geom_jitter()将点添加到箱形图中
# 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包中的调色板
p10 <- p8 + scale_color_brewer(palette="Dark2")
# scale_color_grey:使用灰色调色板
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
# 使用自定义调色板
p14 <- p13 +scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用brewer调色板
p15 <- p13 +scale_fill_brewer(palette="Dark2")
# 使用灰度
p16 <- p13 + scale_fill_grey() + theme_classic()
ggarrange(p13,p14,p15,p16,nrow = 1)

1.5 更改图例中的项目顺序
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()
# 按组自动更改颜色
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()
# 连续色彩
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
小提琴图类似于箱形图,不同之处在于它们还显示了不同值的数据的核概率密度。 通常,小提琴图将包括数据中位数的标记和指示四分位数范围的框,如在标准框图中一样。
函数geom_violin()用于生成小提琴图。
2.1 语法
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 基本小提琴图
# 将数值型变量转换为因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 基本小提琴图
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin()
p
# 旋转
p1 <- p + coord_flip()
# 将trim参数设置为FALSE
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim=FALSE)
# 选择展示数据
p3 <- p + scale_x_discrete(limits=c("0.5", "2"))
# library(ggpubr)
ggarrange(p,p1,p2,p3,nrow = 1)

2.3 在小提琴图上添加摘要统计信息
# 添加平均值
p4 <- p + stat_summary(fun.y=mean, geom="point", shape=23, size=2)
# 添加中位数
p5 <- p + stat_summary(fun.y=median, geom="point", size=2, color="red")
# 添加中位数和四分位数,也即是箱线图
p6 <- p + geom_boxplot(width=0.1)
# 添加平均值和标准偏差
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)

# 产生汇总统计的功能 (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 带点小提琴图
# 带点小提琴图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
# 使用自定义调色板
p13 <- p12 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用Brewer调色板
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)

# 使用自定义调色板
p18 <- p17 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用Brewer调色板
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 更改图例中的项目顺序
# 更改图例中的项目顺序
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))
# 添加点
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 定制小提琴图
## 定制小提琴图
# 基础小提琴图
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()
# 连续型颜色
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
网友评论