以下代码:
library(ggplot2)
theData <- data.frame(category <- sample(LETTERS[1:3], 1000, replace = T),
value <- rnorm(1000))
thePlot <- ggplot(theData,
aes(x = category, y = value))
thePlot <- thePlot + geom_violin(fill = "BLACK")
thePlot <- thePlot + coord_flip()
print(thePlot)
Run Code Online (Sandbox Code Playgroud)
会产生这个情节:

但是我希望达到这样的效果,即在较不密集的区域中每个小提琴密度的填充(和理想的颜色)的α值减小.也就是说,小提琴形状逐渐消失在曲线高度较小的背景中,但是在曲线高的地方是暗的和不透明的.像这种效果的东西:

不幸的是,这些系数图是通过使用非常丑陋的黑客产生的,并且考虑到新geom_violin的灵活性,我想知道是否有一种直接的方法来实现这种使用geom_violin的alpha淡入淡出.
感谢您提供的任何见解!
受到@wch 的回答的启发,我决定再尝试一下。这是我得到的最接近的结果:
ggplot(theData, aes(x = category, y = value)) +
stat_ydensity(geom="segment", aes(xend=..x..+..scaled../2,
yend=..y.., alpha=..scaled..), size=2, trim=FALSE) +
stat_ydensity(geom="segment", aes(xend=..x..-..scaled../2,
yend=..y.., alpha=..scaled..), size=2, trim=FALSE) +
scale_alpha_continuous(range= c(0, 1)) +
coord_flip() + theme_bw()
Run Code Online (Sandbox Code Playgroud)
在早期版本中ggplot2,情节显示“乐队”,但这个问题现在显然已经得到解决。
