rggplot2ggridgesridgeline-plot

In ggridges, how to colour code points without getting multiple ridges?


I'm trying to show points on my ridge plot by an index (PASS or FAIL in this case). But when I do that, I end up with two ridges per group. I am wondering how to have only one ridge per group?

My code, and the image of output below:

library(ggplot2)
library(ggridges)
library(scales)
ggplot(dataSet, aes(x = `Vector_fails`, y = Group, fill = Group)) +
  geom_density_ridges(alpha = .1, 
                      point_alpha = 1,
                      jittered_points = TRUE,
                      aes(point_color = PassCode)) +
  labs(title = 'Vector_fails') +
  scale_x_log10(limits = c(0.09, 1000000), 
                breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides="b")

Two ridges per group (but I want one)

enter image description here


Solution

  • If I understand correctly, the OP wants to plot a ridgeline for each Group regardless of PassCode. In addition, the the points should be plotted but coloured by the value of PassCode.

    One possible solution is to plot the ridgelines without points and then to add a second layer where the points are plotted but the (then duplicated) ridgelines have been made invisible:

    library(ggplot2)
    library(ggridges)
    library(scales)
    ggplot(dataSet, aes(x = `Vector_fails`, y = Group, fill = Group)) +
      geom_density_ridges(alpha = .1) + # plot rigdelines without points
      geom_density_ridges(alpha = 0, # plot points with invisible ridgelines
                          color = NA, 
                          jittered_points = TRUE, 
                          point_alpha = 1, 
                          aes(point_color = PassCode)) +
      labs(title = 'Vector_fails') +
      scale_x_log10(limits = c(0.09, 1000000), 
                    breaks = trans_breaks("log10", function(x) 10^x),
                    labels = trans_format("log10", math_format(10^.x))) +
      annotation_logticks(sides="b")
    

    enter image description here

    Data

    As the question does not include any reproducible data I have tried to simulate OP's dataSet

    nr <- 1000L
    set.seed(1L)
    dataSet <- data.frame(
      Vector_fails = sample(9L, nr, TRUE) * 10^sample(c(0:1, 4:5), nr, TRUE),
      Group = sample(c("Normal", "Trial 1", "Trial 2"), nr, TRUE, prob = c(0.8, 0.1, 0.1)),
      PassCode = sample(c("PASS", "FAIL"), nr, TRUE)
    )