wilkelab / ggridges

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

point_color aesthetic behaves differently when continous versus discrete #29

Closed joachim-gassen closed 5 years ago

joachim-gassen commented 5 years ago

Thank you for this fabulous package!

I came across something and I am not entirely sure whether it is a bug or a feature: When using jittered_points = TRUE and aes(point_colors) in geom_density_ridges() the effect varies dependent on whether the variable assigned to point_colors is continuous or discrete.

ibrary(ggplot2)
library(ggridges)
set.seeed(42)

df <- expand.grid(x = 1:100, y = letters[1:5], z = 1:3)
df$x <- apply(df, 1, function(df) rnorm(1, which(letters == df['y']), as.integer(df['z'])))

# Generates one density for each y as expected

ggplot(df, aes(x, y)) +
  geom_density_ridges(aes(point_color = z), 
                      jittered_points = TRUE)

rplot

# Generates separate densities for each value of z. Why?

df$z <- as.factor(df$z)

ggplot(df, aes(x, y)) +
  geom_density_ridges(aes(point_color = z), 
                      jittered_points = TRUE)

rplot01

Also, the latter plot contains a legend while the former does not. Thank you for any insights on what is causing/explaining this behavior.

clauswilke commented 5 years ago

Whenever you map a discrete variable to a color, ggplot2 automatically sets a group. You'll have to override that to get the result you want.

library(ggplot2)
library(ggridges)
#> 
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#> 
#>     scale_discrete_manual
set.seed(42)

df <- expand.grid(x = 1:100, y = letters[1:5], z = 1:3)
df$x <- apply(df, 1, function(df) rnorm(1, which(letters == df['y']), as.integer(df['z'])))

df$z <- as.factor(df$z)

ggplot(df, aes(x, y)) +
  geom_density_ridges(aes(point_color = z, group = y), jittered_points = TRUE)
#> Picking joint bandwidth of 0.504

Created on 2019-01-16 by the reprex package (v0.2.1)

joachim-gassen commented 5 years ago

You live and learn. Thank you!

akarlinsky commented 5 years ago

I just wanted to say that I was looking for ages for a plot similar to Claus's last comment. So first of all: thank you!

Second, I think maybe adding to the vignette that aes(point_color=var) creates separate densities for each level of var if var is not continuous, and that this can be disabled by passing group=y back in aes() - would be extremely helpful.