Home > Blockchain >  ggplot: multiple time periods on same plot by month
ggplot: multiple time periods on same plot by month

Time:12-09

I am trying to plot multiple time-periods on the same time-series graph by month. This is my data: enter image description here

This actually works good visually as the x axis goes from October to June as desired; however, I did it by setting limits,

startTime <- as.Date("2016-10-01")
endTime <- as.Date("2017-06-30")
start_end <- c(startTime,endTime)

and then setting breaks of 1 month.

scale_x_date(limits=start_end,breaks=date_breaks("1 month"),labels=date_format("%d %b")) 

It is needless to say that this technique will not work if I would like to include other winter seasons and a legend.

I also tried to assign a season to certain time periods and then use them as a factor:

master_dataset <- master_dataset %>%
  mutate(season = case_when(date>=as.Date('2015-11-02')&date<=as.Date('2016-06-30')~"season 2015-16",
                            date>=as.Date('2016-11-02')&date<=as.Date('2017-06-30')~"season 2016-17",
                            date>=as.Date('2017-10-13')&date<=as.Date('2018-06-30')~"season 2017-18",
                            date>=as.Date('2018-10-18')&date<=as.Date('2019-06-30')~"season 2018-19"))

ggplot(master_dataset, aes(month(date, label=TRUE, abbr=TRUE), fl_all_cumsum, group=factor(season),colour=factor(season))) 
  geom_line() 
  labs(x="Month", colour="Season") 
  theme_classic()

enter image description here

As you can see, I managed to include the other seasons in the graph but there are several issues now:

  1. grouped by month it aggregates the daily values and I lose the daily dynamic in the graph (look how it is based on monthly steps)
  2. the x-axis goes in chronological order which messes up my visualization (remember I care for the winter season development so I need the x-axis to go from October-End of June; see the first graph I produced)
  3. Not big of an issue but because the data has NA gaps, the legend also shows a factor "NA"

I am not a programmer so I can't wrap my mind around on how to code for such an issue. In a perfect world, I would like to have something like the first graph I produced but with all winter seasons included and a legend. Does someone have a solution for this? Thanks in advance.

Zorin

CodePudding user response:

This is indeed kind of a pain and rather fiddly. I create "fake dates" that are the same as your date column, but the year is set to 2015/2016 (using 2016 for the dates that will fall in February so leap days are not lost). Then we plot all the data, telling ggplot that it's all 2015-2016 so it gets plotted on the same axis, but we don't label the year. (The season labels are used and are not "fake".)

## Configure some constants:
start_month = 10  # first month on x-axis
end_month = 6     # last month on x-axis
fake_year_start = 2015  # year we'll use for start_month-December
fake_year_end = fake_year_start   1 # year we'll use for January-end_month
fake_limits = c(   # x-axis limits for plot
  ymd(paste(fake_year_start, start_month, "01", sep = "-")),
  ceiling_date(ymd(paste(fake_year_end, end_month, "01", sep = "-")), unit = "month")
)

df = df %>%
  mutate(
    ## add (real) year and month columns
    year = year(date),  
    month = month(date),
    ## add the year for the season start and end
    season_start = ifelse(month >= start_month, year, year - 1),
    season_end = season_start   1,
    ## create season label 
    season = paste(season_start, substr(season_end, 3, 4), sep = "-"),
    ## add the appropriate fake year
    fake_year = ifelse(month >= start_month, fake_year_start, fake_year_end),
    ## make a fake_date that is the same as the real date
    ## except set all the years to the fake_year
    fake_date = date, 
    fake_date = "year<-"(fake_date, fake_year)
  ) %>% 
  filter(
    ## drop irrelevant data
    month >= start_month | month <= end_month,
    !is.na(fl_all_cumsum)
  )


ggplot(df, aes(x = fake_date, y = fl_all_cumsum, group = season,colour= season)) 
  geom_line() 
  labs(x="Month", colour = "Season") 
  scale_x_date(
    limits = fake_limits,
    breaks = scales::date_breaks("1 month"),
    labels = scales::date_format("%d %b")
  )  
  theme_classic()

enter image description here

  • Related