代码之家  ›  专栏  ›  技术社区  ›  Randoms

ggplot2:带有Stat=“Identity”的小提琴绘图

  •  1
  • Randoms  · 技术社区  · 7 年前

    我想用 ggplot 要创建一个小提琴图,其中小提琴的宽度不是由密度函数控制的,而是直接表示相关元素的数量。

    我想这可以通过设置 geom_violin(stat="identity")

    > ggplot(allData, aes(x = tool, y = length)) + geom_violin(stat="identity")
    Warning: Ignoring unknown parameters: trim, scale
    Error in eval(substitute(list(...)), `_data`, parent.frame()) : 
      object 'violinwidth' not found
    

    正在尝试添加 aes(violinwidth=0.2*count) ,作为 this answer 建议,给予

    > ggplot(allData, aes(x = tool, y = length)) + geom_violin(stat="identity", aes(violinwidth=0.2*count))
    Warning: Ignoring unknown parameters: trim, scale
    Warning: Ignoring unknown aesthetics: violinwidth
    Error in FUN(X[[i]], ...) : object 'count' not found
    

    violinwidth 对于一个常数,这使得小提琴只是矩形。我该怎么解决?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jonny Phelps    7 年前

    当我用一些样本数据运行这个程序时,它生成的绘图可以在 stat violinwidth . 是你的吗 count 一列 allData ?

    library(ggplot2)
    
    dt <- data.frame(category = rep(letters[1:2], each = 10),
                     response = runif(20),
                     count = rpois(20, 5))
    
    ggplot(dt, aes(x = category, y = response)) + geom_violin()
    
    ggplot(dt, aes(x = category, y = response)) + 
      geom_violin(stat = "identity", aes(violinwidth = 0.1*count))
    
    推荐文章