Can scale_colour_gradientn and scale_colour_stepsn be used for date, datetime or time variables?

Hi,

Can scale_colour_gradientn and scale_colour_stepsn be used for date, datetime or time variables?

If yes, can someone please illustrate how to do this using the below example?

library(tidyverse)
library(nycflights13)

df <- flights %>% 
  slice_sample(n = 100) %>% 
  mutate(date = as.Date(time_hour))

df %>%  
  ggplot() +
  geom_point(aes(x = origin, y = dep_delay, col = time_hour)) 

Thanks
David

Is this the sort of thing you want?

library(tidyverse)
library(nycflights13)

df <- flights %>% 
  slice_sample(n = 100) %>% 
  mutate(date = as.Date(time_hour))

df %>%  
  ggplot() +
  geom_point(aes(x = origin, y = dep_delay, col = time_hour)) 
#> Warning: Removed 3 rows containing missing values (geom_point).

Apr01 <- lubridate::ymd_hms("2013-04-01 00:00:00")
Jul01 <- lubridate::ymd_hms("2013-07-01 00:00:00")
Oct01 <- lubridate::ymd_hms("2013-10-01 00:00:00")
df %>%  
  ggplot() +
  geom_point(aes(x = origin, y = dep_delay, col = time_hour)) +
  scale_color_gradient(low="red", high = "blue",
                       breaks = c(Apr01,Jul01,Oct01),
                       labels = c("Apr 01", "Jul 01", "Oct 01"))
#> Warning: Removed 3 rows containing missing values (geom_point).

Created on 2022-09-09 with reprex v2.0.2

2 Likes

This topic was automatically closed 7 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.