0

I have a large dataset with dates and times in the POSIXct format. This basically spans two academic years.

#Earliest / last log 
#2018-09-02 14:46:56
#2020-10-12 23:30:13 

I managed to add an academic Year True / False column to split the two years at the 1st of September 2019:

MViews$AcademicYear1819 <- MViews$earliestlog <= as.Date("2019-09-01 00:00:00")

When I graph this ggplot puts everything chronologically on the x axis, but I would like to overlay the two academic years over each other.

MViews %>% 
  ggplot(aes(x = earliestlog, fill = "red")) + geom_density(alpha = 0.5)

So basically that September 2018 and September 2019 both start at x = 0.

I tried loads of things, but nothing worked unfortunately. I even tried just adding one year to all the values returning true on being the 1 year earlier academic year, but that works neither somehow.

MViews$EL <- MViews$EL[MViews$AcademicYear1819] %m+% years(1) & MViews$EL[!MViews$AcademicYear1819]

Error in UseMethod("reclass_date", orig) : no applicable method for 'reclass_date' applied to an object of class "NULL" In addition: Warning messages: 1: Unknown or uninitialised column: EL.

Simon
  • 45
  • 4
  • 1
    Make a "fake date" column where all the September-December have the same year (say, 2018), and all the January-August dates have the next year (2019), then put the fake date column on the x-axis. – Gregor Thomas Jan 11 '22 at 14:16
  • If you need more help than that, please add enough data to reproduce the issue, preferably in a copy/pasteable way, `dput(MViews[1:10, ])` will give us a copy/pasteable version of the first 10 rows - choose a suitable subset. – Gregor Thomas Jan 11 '22 at 14:17
  • see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Jan 19 '22 at 22:12

1 Answers1

0

Couldn't tell if you wanted same graph overlay or graphs on top as in a facet. If just one graph, remove the facet bit below. I also used a 3 year span just for demo.

library(lubridate)


#Generate dataset
set.seed(123)
df <- sample(seq.POSIXt(from =as.POSIXlt('2018-09-02 14:46:56'), to = as.POSIXlt('2020-10-12 23:30:13'), by = "hour"), 1000, replace = TRUE)
df

#Create semester objects to exclude summer months-- step may not be necessary
fallstr = c("Sep", "Oct", "Nov","Dec")
springstr = c("Jan", "Feb","Mar","Apr")


df %>% 
    as_tibble() %>% 
    mutate(year = year(value),
           month = month(value, label = TRUE)) %>% 
#Removing summer months
    filter(month %in% c(fallstr,springstr)) %>% 
#Reordering factor levels used for graphing x-axis later
    mutate(month = factor(month, levels = c(fallstr,springstr))) %>%
#Reclassifying cases into academic years 
    mutate(academic_year = case_when(
        month %in% fallstr & year == 2018 ~ "Year 1",
        month %in% springstr & year == 2019 ~ "Year 1",
        month %in% fallstr & year == 2019 ~ "Year 2", 
        month %in% springstr & year == 2020 ~ "Year 2",
        #May not need year 3 but just included for demo purpose
        month %in% fallstr & year == 2020 ~ "Year 3")) %>% 
#Can use if you want visualize the year, just swap-comment with other ggplot
    #ggplot(aes(x = month, fill = as.factor(year))) +
    ggplot(aes(x = month, fill = academic_year)) +
    geom_histogram(position = "identity", bins =4, alpha = .4, stat = "count") +
    facet_wrap(.~academic_year, ncol = 1)

Please let me know if you had something else in mind

Jonni
  • 804
  • 5
  • 16