Violin (geom_violin) with aggregated values

I would like to create violin sections with aggregated data. My data has a category, value column and count column:

data <- data.frame(category = rep(LETTERS[1:3],3), value = c(1,1,1,2,2,2,3,3,3), count = c(3,2,1,1,2,3,2,1,3)) 

If I create a simple violin plot, it will look like this:

 plot <- ggplot(data, aes(x = category, y = value)) + geom_violin() plot 


(source: ahschulz.de )

This is not what I wanted. The solution is to change the shape of the data frame by multiplying the lines of each category-value combination. The problem is that my calculations are in the millions, and it takes hours to schedule! :-(

Is there a solution with my data?

Thank you in advance!

+4
source share
2 answers

You can imagine the weight when calculating areas.

 plot2 <- ggplot(data, aes(x = category, y = value, weight = count)) + geom_violin() plot2 

A warning message appears that weights are not added to one, but this is normal. See here for a similar / related discussion .

enter image description here

+6
source

Using stat="identity" and defining the aesthetics of violinwidth seems to work, although I had to introduce a fudge factor:

 ggplot(data, aes(x = category, y = value)) + geom_violin(stat="identity",aes(violinwidth=0.2*count)) 
+2
source

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


All Articles