Overlay violin graphs ggplot2

I would like to build two series of ten violin scenes per second:

library(ggplot2) #generate some data coco1<-rnorm(10000,0,1) coco2<-c(runif(10000)) decile<-rbinom(10000,9,1/2)+1 coconut<-data.frame(coco1,coco2,decile) #draw the violin plots of the coco1 serie p <- ggplot(coconut, aes(factor(decile), coco1)) p<-p + geom_violin(aes(alpha=0.3,colour="#1268FF")) p #draw the violin plots of the coco2 serie q <- ggplot(coconut, aes(factor(decile), coco2)) q<-q + geom_violin(aes(alpha=0.3,colour="#3268FF")) q 

I would like to plot the violin graph ā€œpā€ and ā€œqā€ on the same graph, and I want each violin graph ā€œqā€ to be above the corresponding violin graph ā€œpā€.

+6
source share
1 answer

You can simply add geom_violin second plot to the first:

 p <- ggplot(coconut, aes(factor(decile), coco1)) p <- p + geom_violin(aes(colour = "#1268FF"), alpha = 0.3) q <- p + geom_violin(aes(y = coco2, colour = "#3268FF"), alpha = 0.3) 

Now q contains both versions of the violin. enter image description here

By the way: if you want to get rid of the color legend, you need to specify colour outside aes .

+7
source

Source: https://habr.com/ru/post/956372/


All Articles