Molly Wilson
Molly Wilson

Reputation: 3

Extending geom_rect() infinitely along the x-axis with date (POSIXct format) as x-axis variable

I am putting together plots of water quality parameters over time and want to use geom_rect() to illustrate target ranges. As an example, the dissolved oxygen portion of my data looks like this:

head(data_mon %>% select(date, site_code, do_mg_l))

  date                site_code do_mg_l
  <dttm>              <chr>       <dbl>
1 2024-06-03 00:00:00 MRYC         6.48
2 2024-06-03 00:00:00 LDB          7.32
3 2024-06-03 00:00:00 BDB          6.72
4 2024-06-03 00:00:00 YIN          6.25
5 2024-06-03 00:00:00 FHB          6.96
6 2024-06-03 00:00:00 COV          6.57

My target graph looks like this, but would have the reference rectangle extended to the edge of the graph along the x-axis.

ref_do = data.frame(ymin = 6, ymax = Inf) # recommended DO is above 6 mg/L

ggplot() +
    geom_rect(data = ref_do, 
              aes(xmin = min(data_mon$date), xmax = max(data_mon$date), ymin = ymin, ymax = ymax, fill = "What"),
              alpha = 0.4) +
    scale_fill_manual(values = "slategray", guide = "none") +
    geom_line(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "lightblue3") +
    geom_point(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "slategray") +
    scale_x_datetime(date_breaks = "1 month",
                   date_labels = "%b") +
    labs(x = "Sampling date", y = expression("Dissolved oxygen (mg "*L^-1*")")) +
    theme_bw()

Base graph

Normally I would extend the rectangle by setting xmin and xmax within geom_rect() to -Inf and Inf, respectively, but this does not seem compatible with the POSIXct format of the geom_line() and geom_point() datasets. I can extend the rectangle by setting xmin and xmax to earlier/later dates, but this also extends the entire x axis.

ggplot() +
    geom_rect(data = ref_do, 
              aes(xmin = as.POSIXct("2020-01-01"), xmax = as.POSIXct("2030-01-01"), ymin = ymin, ymax = ymax, fill = "What"),
              alpha = 0.4) +
    scale_fill_manual(values = "slategray", guide = "none") +
    geom_line(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "lightblue3") +
    geom_point(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "slategray") +
    scale_x_datetime(date_breaks = "1 month",
                   date_labels = "%b") +
    labs(x = "Sampling date", y = expression("Dissolved oxygen (mg "*L^-1*")")) +
    theme_bw()

Can get rectangle to extend along x axis, but not all the way to edges

If I use xlim() to restrict the x axis, I lose the rectangle entirely.

ggplot() +
    geom_rect(data = ref_do, 
              aes(xmin = as.POSIXct("2020-01-01"), xmax = as.POSIXct("2030-01-01"), ymin = ymin, ymax = ymax, fill = "What"),
              alpha = 0.4) +
    scale_fill_manual(values = "slategray", guide = "none") +
    geom_line(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "lightblue3") +
    geom_point(data = data_mon,
            aes(x = date, y = do_mg_l, group = site_code),
            color = "slategray") +
    scale_x_datetime(date_breaks = "1 month",
                   date_labels = "%b") +
    labs(x = "Sampling date", y = expression("Dissolved oxygen (mg "*L^-1*")")) +
    xlim(min(data_mon$date), max(data_mon$date)) +
    theme_bw()

Trying to take previous graph and just trim x axis, but rectangle disappears

Any advice would be greatly appreciated, thank you!

Upvotes: 0

Views: 44

Answers (1)

Michiel Duvekot
Michiel Duvekot

Reputation: 1881

You should be able to set the min and max x of your geom_rect to -Inf and Inf if you use as.POSIXct().

geom_rect(data = ref_do, aes(xmin = as.POSIXct(-Inf), xmax = as.POSIXct(Inf), ymin = ymin, ymax = ymax, fill = "What"), alpha = 0.4)

Upvotes: 0

Related Questions