wilkelab / ggridges

Ridgeline plots in ggplot2
https://wilkelab.org/ggridges
GNU General Public License v2.0
411 stars 31 forks source link

Feature request: Rainclouds #15

Closed heinonmatti closed 6 years ago

heinonmatti commented 6 years ago

Hi,

I came across this: https://micahallen.org/2018/03/15/introducing-raincloud-plots/

Would it be possible to plot the points below the ridges, instead of inside them?

Many thanks for the great work,

Matti

clauswilke commented 6 years ago

That's fairly simple to implement in principle. My main concern is that the API is getting cluttered by introducing all these different parameter options in geom_density_ridges() and geom_stat_ridges(). Maybe I'd have to introduce a separate geom, e.g. geom_raincloud().

clauswilke commented 6 years ago

I realized that the correct way to implement this is via a position argument. Would you mind trying this out?

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE, position = "raincloud", alpha = 0.7)
screen shot 2018-04-01 at 1 12 35 am
ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE, alpha = 0.7, scale = 0.8, point_size = 0.5,
                      position = position_raincloud(width = 0.05, height = 0.2)) 
screen shot 2018-04-01 at 1 19 03 am

Also, the same code has enabled other options, such as this one:

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE, alpha = 0.7, point_size = 3, point_shape = "|",
                      position = position_points_jitter(width = 0.05, height = 0)) 
screen shot 2018-04-01 at 1 21 19 am
modche commented 6 years ago

This is really nice work (examples work for me)... and makes the package even more usable... Do you think a position = "boxplot" would also be possible...?

clauswilke commented 6 years ago

Position adjustments can only move things around that are already drawable. To draw boxplots, I'd have to add that somehow to geom_ridgeline(), and I'm hesitant to do so. The ggplot2 approach would be to have this in a separate geom, and that works already unless you want the density distributions to partially overlap with boxplots, which is not necessarily useful anyways.

However, we can position adjust the quantile lines. That's probably as far as I'm willing to go with adding extra drawing elements to the geom.

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(jittered_points = TRUE, quantile_lines = TRUE, scale = 0.9, alpha = 0.7,
                      vline_size = 1, vline_color = "red", point_size = 0.4,
                      position = position_raincloud(adjust_vlines = TRUE))
screen shot 2018-04-01 at 11 26 14 am
heinonmatti commented 6 years ago

Exactly what I needed, perfect, many thanks!

@modche, I used userfriendlyscience::diamondPlot with returnLayerOnly = TRUE and otherAxisCol = (1:4 - .25), to overlay separately calculated (my data is clustered, so vanilla ones won't do) means + confidence intervals on plot:

image

heinonmatti commented 6 years ago

... actually, having the diamonds on the density plots might make more sense here:

image

heinonmatti commented 6 years ago

Seems like the points get cut off by the x-axis in the lowest of the four plots?

clauswilke commented 6 years ago

If the axes are cut off you may have to expand them manually using the various avenues ggplot2 provides for this. Unfortunately ggplot2 sometimes gets the axis range wrong, the same can be observed at the very top of the top ridgeline. There's currently no way from within ggridges to fix this.

clauswilke commented 6 years ago

I think this issue is fully addressed now.