r – ggplot : ploting the evolution of a value within a time serie (MM:DD) through the years

The reason why you get an empty chart is that you missed to set the group aesthetic which as more or less general rule is necessary for a line or area chart in cases where the x variable is a discrete or categorical variable as your Date_without_year.

However, as you want to create a chart showing and comparing snow height over ski seasons I would suggest to stick with proper dates to get the right order. But instead of using the true dates fix the years, e.g. use the year 2017 for December and 2018 for the other months. Afterwards to could format the dates via scale_x_date.

Finally, in my opinion a stacked area chart does not make that much sense to compare or visualize snow height over ski seasons. Instead I would suggest to use a line chart.

library(dplyr)
library(ggplot2)

DAll <- DAll |>
  mutate(
    Date_with_year = if_else(
      grepl("^12", Date_without_year),
      paste0("2017-", Date_without_year),
      paste0("2018-", Date_without_year)
    ),
    Date_with_year = as.Date(Date_with_year)
  )

base <- ggplot(DAll, aes(
  x = Date_with_year, y = SnowHeight,
  group = Season
)) +
  scale_x_date(date_labels = "%m-%d") +
  labs(
    x = "Date", y = "Snow Height",
    title = "Snow Height Evolution (Dec-Apr) by Season"
  )

base +
  geom_area(aes(fill = factor(Season)))


base +
  geom_line(aes(color = factor(Season)))

DATA

DAll <- structure(list(Date = c(
  "2017-12-01", "2017-12-02", "2017-12-03",
  "2018-04-28", "2018-04-29", "2018-04-30", "2018-12-01", "2018-12-02",
  "2018-12-03", "2019-04-28", "2019-04-29", "2019-04-30"
), SnowHeight = c(
  33,
  37.5, 39.5, 144, 132, 120, 21, 21.2, 24, 99, 86, 85
), Season = c(
  2018L,
  2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L,
  2019L, 2019L
), Date_without_year = c(
  "12-01", "12-02", "12-03",
  "04-28", "04-29", "04-30", "12-01", "12-02", "12-03", "04-28",
  "04-29", "04-30"
)), class = "data.frame", row.names = c(NA, -12L))

Read more here: Source link