Home > Back-end >  Set different color line for mean and quantile in geom density ridges plot
Set different color line for mean and quantile in geom density ridges plot

Time:08-11

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" ))


my plot here

Do you have any suggestions ?

CodePudding user response:

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

  • Related