geom_tile: ignore out of bounds datetime values

I have datetime values on the x-axis, however there are a large number of out of bound dates, which I can't work out how to ignore (cannot filter as there are no NA values). I'm trying to compare 15 days of data from the start of April last year to the first 15 days of April this year.

I'm thinking there is something simple for this but can't work it out.

library(tidyverse)
library(tidyquant)

  
df <- tibble::tribble(
                ~settlementdate,     ~duid,     ~w_mean_price,
                   "2021-04-01", "GSTONE6",  419.836315789474,
                   "2021-04-02", "GSTONE6",  723.034912280702,
                   "2021-04-03", "GSTONE6",  723.034912280702,
                   "2021-04-04", "GSTONE6",  588.183521126761,
                   "2021-04-05", "GSTONE6",  723.034912280702,
                   "2021-04-06", "GSTONE6",  723.034912280702,
                   "2021-04-07", "GSTONE6",  723.034912280702,
                   "2021-04-08", "GSTONE6",  723.034912280702,
                   "2021-04-09", "GSTONE6",  4794.93122222222,
                   "2021-04-10", "GSTONE6", -515.099508196721,
                   "2021-04-11", "GSTONE6", -246.060877192982,
                   "2021-04-12", "GSTONE6", -229.881147540984,
                   "2021-04-13", "GSTONE6",  379.615540540541,
                   "2021-04-14", "GSTONE6",  405.190579710145,
                   "2021-04-15", "GSTONE6",   1526.3601369863,
                   "2022-04-01", "GSTONE6", -51.1542105263158,
                   "2022-04-02", "GSTONE6",  4217.14140350877,
                   "2022-04-03", "GSTONE6",  6248.64762711864,
                   "2022-04-04", "GSTONE6",  4185.12344262295,
                   "2022-04-05", "GSTONE6",  3273.53553846154,
                   "2022-04-06", "GSTONE6",  3811.31328947368,
                   "2022-04-07", "GSTONE6",  4191.02213114754,
                   "2022-04-08", "GSTONE6",  5459.00894736842,
                   "2022-04-09", "GSTONE6",  4466.37631578947,
                   "2022-04-10", "GSTONE6",  4466.37631578947,
                   "2022-04-11", "GSTONE6",  4180.34836065574,
                   "2022-04-12", "GSTONE6",  4186.02836065574,
                   "2022-04-13", "GSTONE6",   4471.7201754386,
                   "2022-04-14", "GSTONE6",  3933.09738461539,
                   "2022-04-15", "GSTONE6",  4466.37631578947
                )

df <- df %>% 
  mutate(settlementdate = as_datetime(settlementdate))

df %>% 
  ggplot(aes(settlementdate, duid)) +
  geom_tile(aes(fill = w_mean_price), alpha = 1, na.rm = TRUE) +
  
  scale_x_datetime(breaks = scales::date_breaks("1 day"),
                   labels = scales::date_format("%b-%d"),
                   # limits = c(as_datetime("2021-04-01 00:00:00"), as_datetime("2022-04-15 00:00:00")),
                   expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0),
                   labels = c("CPP_4"= expression(bold(CPP_4)))) + #Bold Callide 4
  
  # Aesthetics
  theme_tq() +
  scale_fill_distiller(palette = "Spectral",
                       labels = scales::comma) +
  # scales::oob_squish_any(NA)) +
  
  # Labels
  labs(
    title = "NEM Bidding Behaviour Heatmap - Fossil - Black Coal",
    x = NULL,
    y = "Generating Unit (DUID)",
    caption = "Source: http://nemweb.com.au/Reports/CURRENT/Bidmove_Complete/",
    fill = "Weighted mean\n price/MW"
  ) +
  
  # Theme
  theme(plot.title = element_text(size = 11, face = "bold"),
        axis.title = element_text(size = 10), legend.position = "bottom",
        axis.text.x = element_text(angle = 45, hjust = 1),
        plot.caption = element_text(size = 9, colour = "gray50", face = "italic"),
        plot.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
        panel.background = element_rect(fill = 'gray75'),
        legend.background = element_rect(fill = "antiquewhite"),
        legend.title = element_text(size = 8), legend.text = element_text(size = 7))

Can you do a minimal reproducible example fro the forum users to help you?

I updated the original post.

Leaving settlementdate as character sorts it out - although it does make sorting the dates a little harder.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.