Following a previous question about using hours in the y-axis of ggplot, I would like the hours in the y-axis to be from 19:00 to 07:00, starting at 19:00. All the times in the distress variable are within this range. I am showing trend lines (instead of regression lines, as in the previous question) to better show the issue.
I now have a plot in which the scale of the y-axis goes from 0:00:00 to 23:59:00, and the smooth line is in the middle of the afternoon hours, which is not logical. Here is the plot.
This is the code I used.
#Convert to hours format (do it after removing missings because it makes issues with missing values)
data_clean$distress_time <- as_hms(paste0(data_clean$distress, ':00'))
#Create plot:
lines<-ggplot(data = data_clean, aes(x=survey,y=distress_time, group=id, color=id))+
facet_wrap(~PP_rec)+
geom_line()+
xlab("Survey")+ylab("Distress")+
theme_bw()
lines
#a smoother for the overall group
lines_smooth <- lines + geom_smooth(aes(group = 1), se=FALSE, colour="black", size=1)
lines_smooth
#adjust plot:
smooth_lines2 <- lines_smooth +
labs(x = "Survey", y = "Distress", color = "id") +
scale_y_time(limits = as.hms(c('00:00:00', '23:59:00')), breaks = hms(hours = seq(0, 23.99, 1))) +
scale_x_continuous(breaks = seq(0, 16, 2)) +
theme(text = element_text(size = 10))
smooth_lines2
This is an example of two participants' relevant data (the original database is very large):
structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), levels = c("400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "411", "412", "413", "414", "415", "550", "551", "552", "553", "554", "555", "558", "559", "560", "561", "563", "565", "566",
"567", "568", "569", "570", "571", "572", "573", "574", "575",
"576", "577", "578", "580", "590", "591"), class = "factor"),
distress = c("23:45", "23:30", "23:30", "23:45", "23:30",
"22:30", "23:30", "21:30", "23:45", "22:45", "0:00", "1:00",
"6:15", "0:00", "1:00", "0:00", "2:00", "2:00", "2:00", "1:45"
), survey = c(1, 4, 6, 9, 11, 1, 3, 5, 8, 11, 1, 4, 7, 9,
12, 1, 4, 7, 10, 12)))
#The PP_rec indicates whether it was before or after an event.
How can I set the y-axis scale from 19:00 to 07:00, starting at 19:00, and get a logical smooth line?