sa90210
sa90210

Reputation: 585

Subsetting specific dates in ggplot based of position in dataset

I have made the following plot using ggplot2. I'm wondering whether there is a solution to automatically subset the xmin and xmax values in my geom_rect based on their position in my dataset as opposed to filling in the dates manually. I'm looking to always have xmin as the 6th last date observation in my dataset, and xmax as the last date observation. Similarly, in the scale_x_date sequence I would like to set it to automatically pull the first and last date in the dataset.

ggplot(psce_data, aes(Date,`PSCE Growth`)) +
  geom_rect(aes(xmin = tail(date,1),
                xmax = as.Date('2020-12-01'),
                ymin = 0, ymax = 10), 
            fill = "red", alpha = 0.2) +
  geom_line(size = 1.2, col = '#75002B') +
  scale_x_date(breaks = seq(as.Date('2018-01-01'),as.Date('2020-12-01'),by = '6 months'), date_labels = '%b-%Y') +
  labs(y = 'Year-on-Year Growth (%)') +
  scale_y_continuous(breaks = seq(0,10,by = 2)) +
  theme_bw()

Here is a reproductible of my dataset

structure(list(Date = structure(c(17532, 17563, 17591, 17622, 
17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 
18475, 18506, 18536, 18567, 18597), class = "Date"), `PSCE Growth` = c(6.03806138152698, 
6.03809149124142, 6.01532602228477, 6.14343685180097, 5.72725741494446, 
5.71673506872114, 5.54860173966314, 6.78058899222803, 6.71018505753344, 
7.18229309747457, 7.35166052339761, 6.88491297221491, 6.39557440155487, 
6.04095268041736, 5.98793033021946, 7.0149065226691, 8.3391362577722, 
7.77825490464967, 7.52521947220078, 6.42564285250243, 6.52665779068081, 
6.42119590515603, 6.0627396381368, 7.17023911171296, 7.25116619687204, 
6.14998629821019, 6.10667340304004, 3.70834197649858, 2.18774730704022, 
2.82551654988927, 3.02881235983089, 2.70973404541919, 2.91094424831471, 
2.51635377656063, 2.85799109044056, 1.94656704508046)), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 1

Views: 133

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173858

You can use head(tail(Date, 6), 1):

ggplot(psce_data, aes(Date, `PSCE Growth`)) +
  geom_rect(aes(xmin = head(tail(Date, 6), 1),
                xmax = tail(Date, 1),
                ymin = 0, ymax = 10), 
            fill = "red", alpha = 0.2) +
  geom_line(size = 1.2, col = '#75002B') +
  scale_x_date(breaks = seq(head(psce_data$Date, 1), 
                            tail(psce_data$Date, 1),
                            by = '6 months'), date_labels = '%b-%Y') +
  labs(y = 'Year-on-Year Growth (%)') +
  scale_y_continuous(breaks = seq(0, 10, by = 2)) +
  theme_bw()

enter image description here

Upvotes: 1

Related Questions