rtime-series

How to reformat a plot legend to say actual Dates, but keep DOY?


I am wanting to plot timeseries data of lake depth(y axis) and temperature (x axis) for multiple years (2021-2023).

Due to multiple sample collection dates in one month I want to colour/fill by Day of Year(DOY) to distinguish each sampling date, but I want to reformat the legend bar to say the actual Dates.

I am still new to Stack Overflow so my apologies for my question formatting.

Example DataFrame with type of data I am working with. This is a subset of the year 2021, but I have data for 2021-2023.

library(tidyverse)

Temperature <- tibble(Depth = (c('1' ,'2', '3' , '1', '2', '3')),
Date = c('1/18/2021', '1/18/2021', '1/18/2021', '3/11/2021', '3/11/2021', '3/11/2021'),
DOY = c('18', '18', '18', '70', '70', '70'),
Temp = c('1', '2.5', '3.5', '1.2', '2.0', '2.5'))

Temperature %>%
ggplot(aes(x = Temp, y = Depth, colour = DOY, group = DOY))+
geom_point()+
geom_path()+
scale_y_reverse(limits = c(5,0)) +
scale_x_continuous(limits = c(0,4))+
theme_bw()

I'm also attaching an example of the plot I have.enter image description here


Solution

  • Maybe easiest to make a variable that is a date (in one single year, such as 2024) based on DOY:

    Temperature %>%
      mutate(across(c(Depth, DOY, Temp), as.numeric)) |>
      mutate(Date = mdy(Date)) |>
      mutate(DOY2 = ymd(20231231) + DOY) |>
      
      ggplot(aes(x = Temp, y = Depth, colour = DOY2, group = DOY2))+
      geom_point()+
      geom_path()+
      scale_y_reverse(limits = c(5,0)) +
      scale_x_continuous(limits = c(0,4))+
      theme_bw()
    

    enter image description here

    Or this might be a good place for using the geomtextpath package:

    ...
    geomtextpath::geom_textpath(aes(label = format(DOY2, "%b %d")), 
                                vjust = -0.5) +
    

    enter image description here

    Of if you want to use a custom palette, you may need to specify the date labeling. (Under the hood, dates in R are the number of days since Jan 1, 1970, and sometimes the "dateness" of your number might not get carried over to the color scale.) For example:

    scale_color_viridis_c(labels = \(x) as_date(x, origin = lubridate::origin) |>
                          format("%b %d")) +
    

    enter image description here