1

I'm trying to use ggplot to create a violin plot where instead of the widths of the violins being controlled by a density function, they directly represent the count of relevant elements.

I think this can be accomplished through setting geom_violin(stat="identity"), but R then complains

> 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

Trying to add aes(violinwidth=0.2*count), as this answer suggests, gives

> 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

And while I can set violinwidth to just a constant, this makes the violins just rectangles. How can I fix this?

4

1 回答 1

1

当我使用一些示例数据运行它时,它会生成有和没有对 and 的更改的statviolinwidth。你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))
于 2018-08-14T20:00:06.533 回答