0

I'm working on a temperature data set over time. I used a density ridges plot to visualize my data by month. On each plot I included the median and the mean. I would like to distinguish them by highlighting them in different colors. Here is the code I used and the plot generated. I first used a function named mean_median in order to calculate the mean and the median, then used it in quantile_fun argument. I tried to set two colors in vline_color= but it did not work. (Only one color is used in the code below)

# Function used
    mean_median <- function(x, ...) {
  mean <- mean(x, na.rm=TRUE)
  median <- median(x, na.rm = TRUE)
  c(median, mean)
}

# The plot 
x11();ggplot(BDDMTMAT2, aes (x=Temperature , 
                             y = mois, 
                             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, 
                               alpha = 0.7,
                               quantile_lines=TRUE,
                               vline_color = "green",
                               quantile_fun=function(x,...)mean_median(x)) +
  scale_fill_viridis_c(name = "Température", option = "C")+ 
  scale_y_discrete(limits=c("mai-2022",
                            "avril-2022",
                            "mars-2022",
                            "février-2022",
                            "janvier-2022",
                            "décembre-2021",
                            "novembre-2021",
                            "octobre-2021",
                            "septembre-2021",
                            "août-2021", 
                            "juillet-2021" ))


enter image description here

Do you have any suggestions ?

Phil
  • 7,287
  • 3
  • 36
  • 66
HMK
  • 47
  • 4

1 Answers1

3

One option would be to add your mean and medians via two geom_density_ridges_gradient layers where the second is simply used to add a second and different colored quantile line.

Using the lincoln_weather dataset from ggridges as example data:

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")
#> Picking joint bandwidth of 3.37
#> Picking joint bandwidth of 3.37

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you for your answer it actually solved my problem. However, it seems like the blue line is overlaping on other graphs, how can i avoid the overlaping ? – HMK Aug 10 '22 at 23:41
  • Hm. Should have realized that. The issue is that the second layer is plotted on top of the first. Unfortunately I don't see an option to fix that. So I can only suggest to add this as a `Feature Request` on [Github](https://github.com/wilkelab/ggridges/). Maybe the pkg author has an alternative solution to fix you issue. – stefan Aug 11 '22 at 06:41
  • ok then, i'll try to ask on github as suggested. Thank you for your answers ! – HMK Aug 11 '22 at 19:09