For geom_violin, how is the total area of ​​all violins determined?

When called geom_violininside, ggplot2you can indicate that the area of ​​each violin should be proportional to the number of observations making up this violin by specifying scale="count".

I suppose this works internally by taking some total area (let me call this sum X) and dividing it proportionally among all the violins to be built. This is what I want, except that it can lead to rather narrow violins if there is a significant mismatch between the groups between N, so some groups have a relatively low N. In my case, this just makes the fill look difficult to read.

I think this can be largely resolved, at least in my case, by simply expanding X a bit so that the really small violins get enough area to still be readable. In other words, I want to keep the variation in the area between the violins in accordance with the number of observations, but to increase the “pool” of the total area shared between the violins , so that each gets a little larger.

Does anyone know how to do this? To do this, you need to be a switch. I tried to fuss with arguments geom_violinsuch as width, size, violinwidthand so on, but so far no luck ...

EDIT: The code for the boring, but reproducible sample dataset you can experiment with.

y = runif(100, 1, 10)
x = as.factor(rep(c(1,2), times=50))
z = as.factor(c(rep(1, 10), rep(2, 90)))
df=data.frame(x, y, z)
ggplot(df, aes(x=x, y=y, fill=z)) + geom_violin(scale="count")
+4
1

, width geom_violin. position_dodge, .

ggplot(df, aes(x=x, y=y, fill=z)) + geom_violin(scale="count", width=2)

enter image description here

position_dodge

ggplot(df, aes(x=x, y=y, fill=z)) + geom_violin(scale="count", width=2, position=position_dodge(width=0.5))

enter image description here

+1

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


All Articles