1

I'm trying to make an hypnogram and add color to the wake and sleep stages, something like this:

As you can see the lines are colored

This is an example of the data frame

df <- data.frame (
time = seq(1, 250),
state = c(rep ("AW", 20), rep ("QW",35), rep ("NR", 100), rep ("Rt", 40), rep("R", 55))

And I used this code

library(ggplot2)
library(ggthemes)

ggplot(data = df, aes(x=time, y= state))+
   geom_step(aes(group=1))+
   geom_line(aes(x=time, y= state, colour = state))+
   labs (title = "Subject 1", x = "Time (s)", y= "State")+
   theme_clean()

but I got this

enter image description here

And actually I want that to fill "AW" and "QW" with one color and "Rt" and "R" with another color

This is another example I made with inkscape

enter image description here

3
  • Running your code on the data you provided doesn't produce the figure you provided.
    – Edward
    Commented Jul 3 at 4:37
  • yes, I just used a sample as example. In fact, there are 14 400 observations. The point is that I want the outcome to look like the first image where every step has a color, not like the second where there are lines along the x-axis. Let me know if you need more info. Thanks.
    – Luis Angel
    Commented Jul 5 at 0:32
  • You need to provide a better example, or post a small sample of your data using ‘dput()’
    – Edward
    Commented Jul 5 at 0:45

1 Answer 1

1

Thanks for including your expected outcome; here is one potential approach you could try:

library(tidyverse)

df <- data.frame (
  time = seq(1, 250),
  state = c(rep ("AW", 20), rep ("QW", 35), rep ("NR", 100), rep ("Rt", 40), rep("R", 55)))

df %>%
  mutate(state_numeric = case_when(state %in% c("AW", "QW") ~ 1,
                                   state %in% c("Rt", "R") ~ -1,
                                   state == "NR" ~ 0,
                                   TRUE ~ NA_integer_),
         state = factor(state, levels = c("AW", "QW", "NR", "Rt", "R"),
                        labels = c("W", "W", "NR", "R", "R"))) %>%
  ggplot(aes(x = time, y = state_numeric)) +
  geom_step(aes(group = 1)) +
  geom_line(aes(x = time, y = state_numeric, colour = state)) +
  labs(title = "Subject 1", x = "Time (s)", y = "State") +
  theme_minimal() +
  geom_area(aes(fill = state, colour = state),
            alpha = 0.3) +
  scale_y_continuous(breaks = c(-1, 0, 1), labels = c("R", "NR", "W"))

Created on 2024-07-08 with reprex v2.1.0


Based on your comment (if I just want to color the line, do I only use geom_line? and if I want to identify every state ("AW", "QW", "NR", "Rt", "R")?), perhaps this is what you want?

library(tidyverse)

df <- data.frame (
  time = seq(1, 250),
  state = c(rep ("AW", 20), rep ("QW", 35), rep ("NR", 100), rep ("Rt", 40), rep("R", 55)))

df %>%
  mutate(state_numeric = case_when(state %in% c("AW", "QW") ~ 1,
                                   state %in% c("Rt", "R") ~ -1,
                                   state == "NR" ~ 0,
                                   TRUE ~ NA_integer_),
         state = factor(state, levels = c("AW", "QW", "NR", "Rt", "R"))) %>%
  ggplot(aes(x = time, y = state_numeric)) +
  geom_step(aes(group = 1)) +
  geom_line(aes(x = time, y = state_numeric, colour = state),
            linewidth = 2) +
  labs(title = "Subject 1", x = "Time (s)", y = "State") +
  theme_minimal() +
  scale_y_continuous(breaks = c(-1, 0, 1), labels = c("R", "NR", "W"))

Created on 2024-07-09 with reprex v2.1.0

5
  • This is another example, but I made it with Inkscape
    – Luis Angel
    Commented Jul 5 at 0:44
  • Thanks, if I just want to color the line, do I only use geom_line? and if I want to identify every state ("AW", "QW", "NR", "Rt", "R"), do I use the next line? mutate(state_numeric = case_when(state %in% c("AW", "QW") ~ 1, state %in% c("Rt", "R") ~ -1, state == "NR" ~ 0, TRUE ~ NA_integer_), state = factor(state, levels = c("AW", "QW", "NR", "Rt", "R"), labels = c("W", "QW", "NR", "Rt", "R")))
    – Luis Angel
    Commented Jul 8 at 21:03
  • I think I understand; I have edited my answer - hopefully this gives you your desired outcome @LuisAngel Commented Jul 8 at 23:12
  • Yes, thank you so much
    – Luis Angel
    Commented Jul 9 at 15:32
  • Glad you solved your problem @LuisAngel - sorry I didn't understand your original question Commented Jul 9 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.