ggplot2:Stat ="Identity"的小提琴图. [英] ggplot2: Violin Plot with Stat="Identity"
问题描述
我正在尝试使用ggplot
创建一个小提琴图,在该图中,小提琴的宽度不是由密度函数控制的,而是直接表示相关元素的数量.
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.
我认为可以通过设置geom_violin(stat="identity")
来实现,但是R然后抱怨
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
尝试添加aes(violinwidth=0.2*count)
,如此答案所建议的,给出
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
虽然我可以将violinwidth
设置为一个常数,但这会使小提琴只是矩形.我该如何解决?
And while I can set violinwidth
to just a constant, this makes the violins just rectangles. How can I fix this?
推荐答案
当我使用一些示例数据运行此程序时,无论是否更改stat
和violinwidth
,它都会生成确定的图.您的count
是allData
中的一列吗?
When I run this with some sample data, it generates the plots ok with and without the changes to stat
and violinwidth
. Is your count
a column in 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))
这篇关于ggplot2:Stat ="Identity"的小提琴图.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!