1

I'm working on a temperature data set over time and I used a density ridges plot to visualize my data by month. On each plot I included the median and the mean by using 2 geom_density_ridges_gradient function. The reason why I used two geom_density_ridges_gradient function is because I wanted to distinguish them by two different colors. My issue is that the second layer/function (mean) is plotted on top of the first, causing an overlapping of the blue line (the mean) on others plot.

How can I avoing the overlapping ?

Here's the code below, I used lincoln_weather dataset from ggridges as an example

library(ggridges)
library(ggplot2

ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = Month, fill = stat(x))) +
  geom_density_ridges_gradient(
    alpha = 0.8,
    color = "black",
    scale = 2,
    rel_min_height = 0,
    jittered_points = TRUE,
    position = position_points_jitter(
      width = 0.05,
      height = 0
    ),
    point_shape = "|",
    point_size = 3,
    point_alpha = 1,
    quantile_lines = TRUE,
    vline_color = c("green"),
    quantile_fun = median
  ) +
  geom_density_ridges_gradient(
    scale = 2,
    rel_min_height = 0,
    quantile_lines = TRUE,
    vline_color = c("blue"),
    fill = NA,
    quantile_fun = mean
  ) +
  scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
  labs(title = "Temperatures in Lincoln NE in 2016")

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
HMK
  • 47
  • 4
  • I know this is not the answer you're after, but just make two plots. I don't see a value in what you're trying to graph, not quite sure if it is possible, if I got your question right. – M-- Aug 11 '22 at 20:08
  • The problem is that i'm working on data set from different geographical locations. I already have a lot of plots and I dont want to have more plots to do just to fix another line. – HMK Aug 11 '22 at 20:21
  • I also want to keep that scale = 2 on`scale` argument. It makes the comparison easier between each month – HMK Aug 11 '22 at 20:54
  • I don't think there's a way to do what you want with two layers of ridges. I don't think there's a way to make a single layer do what you want either, as written. I actually think your best bet is to fork the ggridges package and edit the function so it let's you specify different colors for the mean line and the quantile line. – Gregor Thomas Aug 12 '22 at 00:13
  • It would at least be a reasonable feature request for the package to expect/allow the `vline_color` arg to be the same length as the `quantile_fun` output. – Gregor Thomas Aug 12 '22 at 00:21
  • Though perhaps you could work out something with `geom_segment` [as this issue is trying to do?](https://github.com/wilkelab/ggridges/issues/40) – Gregor Thomas Aug 12 '22 at 01:17

0 Answers0