reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allrated.csv", col_names = TRUE)
reads2019 <- reads2019 %>% mutate(date_started = as.Date(reads2019$date_started, format = '%m/%d/%Y'), date_read = as.Date(date_read, format = '%m/%d/%Y'), days_reading = order_by(date_read, cumsum(read_time)), DOY = yday(date_read))
reads2019 <- reads2019 %>% mutate(read_time = ifelse(read_time > DOY, DOY, read_time), days_reading = order_by(date_read, cumsum(read_time)))
reads2019 %>% ggplot(aes(DOY, days_reading)) + geom_point() + scale_x_continuous(breaks = seq(0, 380, 20)) + scale_y_continuous(breaks = seq(0, 360, 20)) + xlab("Day of Year") + ylab("Days Reading")
Another potential use for order_by is with the fill function, which fills missing values from the next or previous column entry. If you were doing a study where people receive repeated measures and miss one of the measurements, or if your dataset only notes a new value when the previous one has changed, you could use fill to copy previous values into the empty cells.
Tomorrow, we'll talk more about the scales package, to enhance your ggplots!