概述:绘制小提琴图时按数据分布的密度填充不同透明度的颜色(渐变填充)。
使用工具:R语言中的ggplot2和tidybayes工具包
本文使用的数据及计算方式与之前的博文一致:数据可视化——R语言ggplot2包绘制精美的小提琴图(并箱线图或误差条图组合)。
本文采用tidybayes包中stat_eye()绘制小提琴图,通过设置aes(alpha = stat(f)可实现渐变填充。由于stat_eye()会默认采用中位数及分位数作为误差条,本文采用均值±标准差的形式呈现误差条,因此屏蔽了stat_eye()默认的误差条显示:设置show_interval = FALSE。与前文一致,本文也用tidybayes包绘制分半的小提琴图,并渐变填充。
小提琴图结合误差条图用于表达数据的代码如下:
rm(list=ls()) #清除工作区
library(ggplot2)
set.seed(1234)
#生成模拟数据
Group <- rep(c("group1","group2"),each=200) #组别变量
Group <- factor(Group) #组别因子化
Attribute <- c(rep("Attribute_1",100),rep("Attribute_2",100),rep("Attribute_1",100),rep("Attribute_2",100)) #每个组别的两个属性
Attribute <- factor(Attribute) #属性因子化
value <- c(rnorm(100)+1,rnorm(100)+2,rnorm(100)+1.2,rnorm(100)+1.5) #随机赋值
Data <- data.frame(Group=Group,Attribute=Attribute,value=value) #生成数据框
#对数据进行统计的函数
#指定分组变量和求值变量后,可计算出不同分组变量(或分组变量间的组合)对应的求值变量的均值,标准差,标准误,置信区间ci
#汇总数据
#计算出计数,平均值,标准差,均值的标准误差和置信区间(默认为95%)
#data:一个数据框
#measurevar:包含要汇总的变量的列的名称
#groupvars:包含分组变量的列名称的向量
#na.rm:一个布尔值,表示是否忽略NA
## conf.interval:置信区间的百分比范围(默认为95%)
## Summarizes data.
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
## data: a data frame.
## measurevar: the name of a column that contains the variable to be summariezed
## groupvars: a vector containing names of columns that contain grouping variables
## na.rm: a boolean that indicates whether to ignore NA's
## conf.interval: the percent range of the confidence interval (default is 95%)
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
conf.interval=.95, .drop=TRUE) {
library(plyr)
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean (xx[[col]], na.rm=na.rm),
sd = sd (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" column
datac <- rename(datac, c("mean" = measurevar))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
#依据分组对vale进行统计
Data_summary <- summarySE(Data, measurevar="value", groupvars=c("Group","Attribute"))
P1 <- ggplot(Data, aes(x=Group, y=value,fill=Attribute)) +
stat_eye(aes(alpha = stat(f)), show_interval = FALSE, trim=FALSE, position=position_dodge(0.9)) + #alpha = stat(f)表示按照数据分布密度选择不同同透明度的颜色填充
geom_point(data = Data_summary,aes(x=Group, y=value),pch=19,position=position_dodge(0.9),size=1.5)+ #绘制均值为点图
geom_errorbar(data = Data_summary,aes(ymin = value-sd, ymax=value+sd), #误差条表示均值±标准差
width=0.1, #误差条末端短横线的宽度
position=position_dodge(0.9),
color="black",
alpha = 0.7,
size=0.5) +
scale_fill_manual(values = c("#56B4E9", "#E69F00"))+ #设置填充的颜色
theme_classic()+
theme(legend.position="none") #不需要图例
P1
jpeg(file = "results_Value_1.jpg",width =1600,height = 2000,units = "px",res =300) #结果保存
print(P1)
dev.off()
分半的小提琴图结合误差条图的代码如下:
P2 <- ggplot(Data, aes(x=Group, y=value,fill=Attribute)) +
stat_halfeye(data = Data[Attribute == "Attribute_1",],aes(alpha = stat(f)), show_interval = FALSE, trim=FALSE,side = "left", width = 0.4) +
stat_halfeye(data = Data[Attribute == "Attribute_2",],aes(alpha = stat(f)), show_interval = FALSE, trim=FALSE,side = "right", width = 0.4) +
geom_point(data = Data_summary,aes(x=Group, y=value),pch=19,position=position_dodge(0.5),size=1.5)+ #绘制均值为点图
geom_errorbar(data = Data_summary,aes(ymin = value-sd, ymax=value+sd), #误差条表示均值±标准差
width=0.1, #误差条末端短横线的宽度
position=position_dodge(0.5),
color="black",
alpha = 0.7,
size=0.5) +
scale_fill_manual(values = c("#56B4E9", "#E69F00"))+ #设置填充的颜色
theme_classic()+
theme(legend.position="none") #不需要图例
P2
jpeg(file = "results_Value_2.jpg",width =1600,height = 2000,units = "px",res =300) #结果保存
print(P2)
dev.off()
绘制分半小提琴图时,Attribute_1的数据采用左半边显示,Attribute_2的数据采用右半边显示,因此,绘制时分两次绘制,但需要控制每次输入的数据,如绘制左边时,仅将Attribute == "Attribute_1"的数据作为输入。
tidybayes包提供了多种用于表示数据分布的绘图方式,包括:小提琴图,分半小提琴图,概率密度图、直方图等多种形式,还可以灵活设置垂直方向或水平方向显示。示例图如下所示。
tidybayes包的更多学习资料请参考:https://mjskay.github.io/tidybayes/articles/slabinterval.html
采用tidybayes包中的stat_ gradient interval()呈现数据的方式如下:
P3 <- ggplot(Data, aes(x=Group, y=value,fill=Attribute)) +
stat_gradientinterval(aes(alpha = stat(f)), show_interval = FALSE, trim=FALSE,position=position_dodge(0.5), width = 0.5) +
geom_point(data = Data_summary,aes(x=Group, y=value),pch=19,position=position_dodge(0.5),size=1.5)+ #绘制均值为点图
geom_errorbar(data = Data_summary,aes(ymin = value-sd, ymax=value+sd), #误差条表示均值±标准差
width=0.1, #误差条末端短横线的宽度
position=position_dodge(0.5),
color="black",
alpha = 0.7,
size=0.5) +
scale_fill_manual(values = c("#56B4E9", "#E69F00"))+ #设置填充的颜色
theme_classic()+
theme(legend.position="none") #不需要图例
P3
jpeg(file = "results_Value_3.jpg",width =1600,height = 2000,units = "px",res =300) #结果保存
print(P3)
dev.off()
References