您的数据采用 list() 格式,因此必须将其转换为数据框。然后通过将值堆叠在一起来融化数据框。
使用geom_violin
我们创建核密度图并使用geom_boxplot
,我们在核密度图之上创建箱线图。箱线图的宽度由 控制 width
。
library('ggplot2')
library('reshape2')
df <- data.frame( lapply(x, function(y) {length(y) <- max(lengths(x)); y})) # create data frame from list of x
colnames(df) <- as.character(labels) # change column names to labels
df <- melt(df) # melt data frame
df <- df[ !is.na(df$value), ] # remove NA
ggplot(data = df ) +
geom_violin(aes(x = variable, y = value, fill = variable )) + # kernel density plot
geom_boxplot(aes(x = variable, y = value ), width = 0.1) + # box plot
xlab( " labels " ) + # x axis title
ylab( " values " ) # y axis title

修剪=假
ggplot(data = df ) +
geom_violin(aes(x = variable, y = value, fill = variable ), trim = FALSE ) + # kernel density plot
geom_boxplot(aes(x = variable, y = value ), width = 0.1) + # box plot
xlab( " labels " ) + # x axis title
ylab( " values " ) # y axis title

数据:
labels=c(10,20,30)
x1=c(1,2,3,4)
x2=c(5,6,7,8,9,10)
x3=c(11,12,13,14,15,16)
x=list(x1,x2,x3)