1

I have built a nice graph that shows daily time patterns per day of the week.

Within each curve for each day of the week I would like to visualise the data by group. Grouping should occur from the column "ID" that I can either set to True/False or A/B or whatever is best. Ideally each graph would then consist of two different colours, showing clearly which group is mostly responsible for certain peaks. Meaning that One group of data points is at the bottom and the other stacked on top.

color = ID, fill = ID did not work.

Any idea how i could achieve this?

  ggplot(aes(x = weekdaytime, y = weekday, fill = weekday, group = weekday, height = ..count..)) +
  geom_density_ridges_gradient(stat = "density", scale = 2, rel_min_height = 0.01, bw = 300 ) +
  scale_fill_viridis(name = "", option = "C") +
  scale_x_datetime(date_breaks = "2 hour", date_labels = "%H", 
                   limits = as.POSIXct(strptime(c(paste(Sys.Date(), "00:00", sep=" "),
                                                  paste(Sys.Date(), "24:00", sep=" ")), 
                                                format = "%Y-%m-%d %H:%M"))) +
  scale_y_discrete(limits=c("Monday", "Tuesday", "Wednesday", "Thursday", 
                            "Friday", "Saturday", "Sunday"))+
  theme(
    legend.position="none",
    panel.spacing = unit(0.1, "lines"),
    panel.grid.major = element_line(colour = "grey"),
    panel.grid.minor = element_line(colour = "grey"),
    
    panel.background = element_blank(),
    strip.text.x = element_text(size = 7)
  )  ```
Simon
  • 45
  • 4
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan May 17 '23 at 23:21

1 Answers1

1

If you introduce another factor into your aes() (e.g. color or shape) it will change the number of density curves, e.g. using the palmerpenguins dataset:

library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  )
#> Picking joint bandwidth of 153

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    aes(point_color = island),
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = "none")
#> Picking joint bandwidth of 172

Created on 2023-05-18 with reprex v2.0.2

So, for the Adelie penguins, the 'groups' (the island of origin) are plotted but you have three density curves instead of the single curve in the first plot.

A potential workaround for this is to plot the ridges and the dots separately using two geom_density_ridges() calls, e.g.

library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges() +
  geom_density_ridges(
    aes(point_color = island),
    alpha = 0, color = NA, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = "none")
#> Picking joint bandwidth of 153
#> Picking joint bandwidth of 172

Created on 2023-05-18 with reprex v2.0.2

Would this approach work for your use-case?

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thank you very much! I did get it to work after removing (height = ..count..) from aes. R throws the following error at me when I include it: Error in `f()`: ! Aesthetics must be valid computed stats. Problematic aesthetic(s): height = ..count... Did you map your stat in the wrong layer? I would like the curves to display absolute values in the height, so that one can see the sample sizes. Any idea how to incorporate that as well? – Simon May 18 '23 at 13:17
  • Not sure sorry; I think it would be worth posting that as a new question @Simon - someone might know a way to do it – jared_mamrot May 18 '23 at 23:38