5

Here is the code I am working with:

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

which generates this image:

enter image description here

Notice how the bodies of the violins create a transparency problem the further down the layers of violins you go? Ideally, I'd like the bodies to have alpha=0 and the outline of the body to have alpha=1.

evoclue
  • 169
  • 2
  • 11
  • 1
    `geom_violin(fill = "transparent")` ? – zx8754 Feb 26 '16 at 21:07
  • Possible duplicate of [How to make graphics with transparent background in R using ggplot2?](http://stackoverflow.com/questions/7455046/how-to-make-graphics-with-transparent-background-in-r-using-ggplot2) – zx8754 Feb 26 '16 at 21:13

1 Answers1

7
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

Roland
  • 127,288
  • 10
  • 191
  • 288
  • It worked. Thanks. I had done fill=F before and it didn't work. NA seems like an obvious alternative. – evoclue Feb 26 '16 at 21:10