【问题标题】:Change x-axis in violin plot in R更改R中小提琴图中的x轴
【发布时间】:2017-08-11 08:26:56
【问题描述】:

我正在尝试更改 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 中。

谢谢你

【问题讨论】:

  • 是的。但前提是我可以传递小提琴中的值列表而不是单独指定每个级别。

标签: r violin-plot


【解决方案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

trim = FALSE

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) 

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 2018-12-10
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2021-03-07
    • 2023-03-06
    相关资源
    最近更新 更多