-1

I want to illustrate mean sleep times per day grouped by different survey phases (T1, T2, T3). Here is an example of what my dataframe looks like:

structure(list(code = c("AJH27", "AJH27", "AJH27", "AJH27", "AJH27", 
"AJH27"), slt = c("22:10:00", "21:22:00", "22:00:00", "21:50:00", 
"20:55:00", "21:55:00"), day = c("Mi", "Do", "Fr", "Sa", "So", 
"Mo"), tmp = c("T1", "T1", "T1", "T1", "T1", "T1")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

The orginal datasets consists the data of more than three observations. Now I want to create a line graph, with the days (Mo to So) on the x-axis showing the mean sleep time for each day of the survey phase (clock time on y-axis). This would include three different lines (colored by group).

I not very experienced using ggplot and I could not figure out how to build the graph I want.

EDIT:

This is what my graph looks like when I used your code @JonSpring

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
psycho95
  • 131
  • 1
  • 12
  • 1
    It's more helpful if you can provide code that produces data like yours. The simplest way might be to use the wonderful `dput` function to translate your data (or a subset thereof) into code. For example, you could use `dput(head(YOUR_TABLE, 27))` to create code that, when we run it, lets us generate the first 27 rows of YOUR_TABLE, verbatim. Otherwise, anyone who wants to help will need to do a few minutes of the same manual work to reformat "what your dataframe looks like" into something they can work with. – Jon Spring Sep 14 '21 at 16:25
  • thank you @JonSpring, I didn't know how to create that code. I updated my question – psycho95 Sep 14 '21 at 16:32

1 Answers1

1

Here's an approach using lubridate::hms to convert the H:M:S text into a time period (in seconds) and then convert that to hours.

library(dplyr); library(lubridate); library(ggplot2)

# convert day into an ordered factor so it displays in the right order
dat$day = factor(dat$day, ordered = TRUE,
                     levels = c("Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"))

dat %>%
  mutate(hours = as.numeric(hms(slt))/(60*60)) %>%
  ggplot(aes(day, hours, color = tmp, group = tmp)) +
  geom_line()

enter image description here

EDIT: alternatively, to use a timestamp for the y axis:

dat %>%
  mutate(hours = ymd_hms(paste("20000101", slt))) %>%
  ggplot(aes(day, hours, color = tmp, group = tmp)) +
  geom_line()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you! But how can I visualize the clock time on the y-axis? Like 21:00, 22:00, 23:00...? – psycho95 Sep 14 '21 at 16:45
  • edited to show a way to make the y axis a date-time (arbitrarily 2000-01-01 here, but day doesn't matter). – Jon Spring Sep 14 '21 at 16:49
  • Unfourtunaly I receive this error: 1: Problem with `mutate()` input `hours`. i 226 failed to parse. i Input `hours` is `ymd_hms(paste("20000101", slt))`. 2: 226 failed to parse. 3: Removed 226 row(s) containing missing values (geom_path). And the graph does not look like the one you posted – psycho95 Sep 14 '21 at 16:56
  • Ok it sounds like some (or all, if your data is 226 rows) of the values in `slt` are not parsing as H:M:S. If you run `dat %>% mutate(hours = ymd_hms(paste("20000101", slt))) %>% view` can you see some examples where `hour` turns into NA, i.e. it is not parsing correctly? We'd need to see what `slt` looks like in those cases to figure out the problem. – Jon Spring Sep 14 '21 at 16:59
  • Does the output look like what I showed when you just use your provided data from the question? – Jon Spring Sep 14 '21 at 17:01
  • if I run `dat %>% mutate(hours = ymd_hms(paste("20000101", slt))) %>% view` it gives me another error, that function `view` could not be found. Maybe it's because of missing data? – psycho95 Sep 14 '21 at 17:03
  • I tried again with `View` instead of `view`. Gives me that: `Warning in View : Problem with `mutate()` input `hours`. i 226 failed to parse. i Input `hours` is `ymd_hms(paste("20000101", slt))`. Warning in View : 226 failed to parse.` The rows with missing hours are NAs of slt – psycho95 Sep 14 '21 at 17:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237102/discussion-between-jon-spring-and-psycho95). – Jon Spring Sep 14 '21 at 17:57