Plot day of year on y-axis but label as month-day in ggplot2

Viewed 98

I'm working with this dataset with 3 variables: year, upwelling, and day:

structure(list(year = c(1982, 1982, 1983, 1983, 1984, 1984, 1985, 
1985, 1986, 1986, 1987, 1987, 1988, 1988, 1989, 1989, 1990, 1990, 
1991, 1991, 1992, 1992, 1993, 1993, 1994, 1994, 1995, 1995, 1996, 
1996, 1997, 1997, 1998, 1998, 1999, 1999, 2000, 2000, 2001, 2001, 
2002, 2002, 2003, 2003, 2004, 2004, 2005, 2005, 2006, 2006, 2007, 
2007, 2008, 2008, 2009, 2009, 2010, 2010, 2011, 2011, 2012, 2012, 
2013, 2013, 2014, 2014, 2015, 2015, 2016, 2016, 2017, 2017, 2018, 
2018, 2019, 2019, 2020, 2020), upwelling = c("First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day", "First Day", "Last Day", "First Day", "Last Day", 
"First Day", "Last Day", "First Day", "Last Day", "First Day", 
"Last Day"), day = c(149L, 193L, 152L, 225L, 153L, 251L, 169L, 
193L, 133L, 210L, 159L, 194L, 181L, 198L, 129L, 219L, 152L, 197L, 
154L, 360L, 147L, 194L, 170L, 214L, 122L, 242L, 137L, 192L, 165L, 
185L, 167L, 303L, 179L, 214L, 148L, 237L, 174L, 200L, 166L, 226L, 
129L, 206L, 138L, 196L, 148L, 199L, 158L, 200L, 140L, 202L, 149L, 
247L, 178L, 186L, 177L, 195L, 167L, 193L, 140L, 223L, 163L, 223L, 
129L, 188L, 143L, 186L, 122L, 156L, 132L, 203L, 141L, 204L, 140L, 
219L, 119L, 198L, 154L, 192L)), row.names = c(NA, -78L), class = c("tbl_df", 
"tbl", "data.frame"))

I made a line plot as below, with y-axis being the day of the year.

 df %>%
 ggplot(aes(x = year, y = day, col = upwelling, group = upwelling)) + 
  geom_point() +
  geom_line() + 
  geom_smooth() +
  scale_x_continuous(breaks = c(1982, 1990, 1995,
                                2000, 2005, 2010, 2015,
                                2020)) 

Image of Plot

How do I label the y-axis month-day format, instead of day of year?

2 Answers

Note! this doesn't answer your question completely yet!

...

One problem is, Month+Day don't fall on the same Year+Day every year (e.g. Leap Year)

...

To figure out the date for day 189 of the year 2016:

as.Date(189, origin = "2016-01-01")
[1] "2016-07-08"

Applied to your data set:

df$date <- as.Date(df$day, origin = paste(df$year, "01-01", sep="-"))

I am not sure how will your final plot would look since you have data for different years but this might be a step towards it.

library(dplyr)
library(ggplot2)

#Change day-year to date and then month-day format.
data <- df %>%
  mutate(date = as.Date(paste(day, year), '%j %Y'), 
         date = format(date, '%b %d')) 
  
ggplot(data, aes(x = year, y = date, col = upwelling, group = upwelling)) + 
  geom_point() +
  geom_line() + 
  geom_smooth() +
  scale_x_continuous(breaks = c(1982, 1990, 1995,
                                2000, 2005, 2010, 2015,
                                2020)) + 
  scale_y_discrete(breaks = data$date[seq(1, nrow(data), 3)])

Since y-axis will be crowded I used data$date[seq(1, nrow(data), 3)] to display every 3rd label.

Related