1

我正在尝试更改 vioplot 的 x 轴中的值。我使用了一个建议并写道:

library(vioplot)  
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)  
do.call(what = vioplot, args = x)  
axis(side=1,at=1:length(labels),labels=labels)  

但似乎 a 轴中的值被添加到我不想呈现的 1-2-3 中。

谢谢你

4

1 回答 1

1

您的数据采用 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) 
于 2017-03-20T07:12:31.187 回答