在ggplot2中用透明体覆盖小提琴图

时间:2016-02-26 21:00:39

标签: r ggplot2

以下是我正在使用的代码:

library(ggplot2)
coco1<-rnorm(10000,0,1)
coco2<-rnorm(10000,10,5)
coco3<-rnorm(10000,20,10)
coco4<-rnorm(10000,30,20)
decile<-rbinom(10000,3,1/2)+1
coconut<-data.frame(coco1,coco2,coco3,coco4,decile)

p <- ggplot(coconut, aes(factor(decile), coco1))
p <- p + geom_violin(aes(colour = "1"), alpha = .5, size=2)
q <- p + geom_violin(aes(y = coco2, colour = "2"), alpha = .5, size=2)
q <- q + geom_violin(aes(y = coco3, colour = "3"), alpha = .5, size=2)
q <- q + geom_violin(aes(y = coco4, colour = "4"), alpha = .5, size=2)

q

生成此图像:

enter image description here

请注意小提琴的身体是如何在你进入的小提琴层中产生透明度问题的?理想情况下,我希望身体的alpha = 0,身体轮廓的alpha = 1。

1 个答案:

答案 0 :(得分:3)

p <- ggplot(coconut, aes(factor(decile), coco1)) + 
  geom_violin(aes(colour = "1"), fill = NA, size=2) + 
  geom_violin(aes(y = coco2, colour = "2"), fill = NA, size=2) +
  geom_violin(aes(y = coco3, colour = "3"), fill = NA, size=2) +
  geom_violin(aes(y = coco4, colour = "4"), fill = NA, size=2)

p

resulting plot