1

There probably is an obvious solution to this, but I'm a bit naive with plotting. I'd like to have point_shape and point_fill defined manually to indicate e.g. gender groups. How would I go about doing this?

In other words, I'd like to see e.g. green squares for females and blue triangles for non-females (as an arbitrary example). (A commenter mentioned that there are no points on the plot, but there are when using the current development version of ggridges.)

# Simulate data:
df <- data.frame(female = factor(sample(0:1, size = 500, replace = TRUE)),
                 intervention = factor(sample(0:1, size = 500, replace = TRUE))) %>% 
  dplyr::mutate(value = ifelse(female == "1", runif(n = 500, min = 0, max = 100), 
                               rnorm(n = 500, mean = 50, sd = 20)))

# Draw plot:
df %>% 
  ggplot2::ggplot(aes(y = intervention)) +
  ggridges::geom_density_ridges2(aes(x = value, 
                                     colour = "black", 
                                     fill = female),
                                 scale = .7,
                                 alpha = 0.6, 
                                 size = 0.25,
                                 jittered_points = TRUE, 
                                 point_shape = 21,
                                 point_size = 0.85,
                                 point_fill = "black")

here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • There are no points in this plot at all. What points are you trying to draw? Are you trying to draw each observation? What y value do you want to use for each x-value? – MrFlick Mar 30 '18 at 20:56
  • Sorry, not sure I understand the part about no points? Added the plot I'm getting with the code, which shows each x-axis observation jittered on y-axis, bounded by the density curve. – Matti Heino Mar 31 '18 at 21:16
  • Ok, so the problem was that I forgot I was using the non-CRAN version of the package. Points come with the developmental version. Thanks for looking into it! – Matti Heino Apr 02 '18 at 08:32

2 Answers2

3

We cannot see the points with the ggridges version currently available on CRAN.

You are using options that are available only from the development version of ggridges available on github :

library(tidyverse)
# Simulate data:
df <- data.frame(female = factor(sample(0:1, size = 500, replace = TRUE)),
                 intervention = factor(sample(0:1, size = 500, replace = TRUE))) %>% 
    dplyr::mutate(value = ifelse(female == "1", runif(n = 500, min = 0, max = 100), 
                                 rnorm(n = 500, mean = 50, sd = 20)))

# devtools::install_github("clauswilke/ggridges")
library(ggridges)

ggplot(df, aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                         scale = .7,
                         alpha = 0.6, 
                         size = 0.25,
                         jittered_points = TRUE, 
                         point_shape = 21,
                         point_size = 0.85,
                         point_fill = "black") 
#> Picking joint bandwidth of 8.06

It seems impossible to map the aesthetics of the points to any variables. They need to be fixed values. I tried to specify manually the shapes but it does not work :

ggplot(df, aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                                   scale = .7,
                                   alpha = 0.6, 
                                   size = 0.25,
                                   jittered_points = TRUE, 
                                   point_shape = c(21,22)[df$female],
                                   point_size = 0.85,
                                   point_fill = "black") 
#> Picking joint bandwidth of 8.06
#> Error: Aesthetics must be either length 1 or the same as the data (2548): size, scale, alpha, point_shape, point_size, point_fill

The best I can do is this not very nice plot :

ggplot(df , aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                         scale = .7,
                         alpha = 0.6, 
                         size = 0.25) +
    geom_point(aes(x = value, shape = female), 
               position = position_jitter(height = 0.2, width = 0))
#> Picking joint bandwidth of 8.06

You could probably post an issue/feature request on github

Created on 2018-04-01 by the reprex package (v0.2.0).

Gilles San Martin
  • 4,224
  • 1
  • 18
  • 31
3

Package author here. What's going on is the following: In the general case, you want to be able to style point color, size, etc. independently from line color, size, etc. However, standard ggplot can't do this. It has only one color aesthetic, for example, that is applied to all points and lines.

To work around this issue I created new aesthetics point_color, point_size, point_shape, etc. that apply specifically to points. You can map data onto them as you normally would. However, ggplot then doesn't have a way to create scales for them, and therefore I created scale_discrete_manual() (and a few other scales) that you can use to define appropriate scales.

Putting all of this together, you arrive at something like this:

# Draw plot:
df %>% 
  ggplot2::ggplot(aes(y = intervention)) +
  ggridges::geom_density_ridges2(aes(x = value, 
                                     point_color = female,
                                     point_fill = female,
                                     point_shape = female,
                                     fill = female),
                                 scale = .7,
                                 alpha = 0.6, 
                                 size = 0.25,
                                 jittered_points = TRUE, 
                                 point_size = 0.85) +
  ggplot2::scale_fill_manual(values = c("#A0FFA0", "#A0A0FF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_color", values = c("#00BF00", "#0000BF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_fill", values = c("#80FF80", "#8080FF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_shape", values = c(22, 24))

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104