I have data that is collected annually that I am trying to plot together. For each year, data is available for some random subset of days. For example:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
set.seed(123)
year1 <- seq(ymd("2019-03-01"), ymd("2019-10-31"), by = "day")
year2 <- seq(ymd("2020-07-01"), ymd("2020-10-31"), by = "day")
year3 <- seq(ymd("2021-03-01"), ymd("2021-10-31"), by = "day")
dat <- tibble(date = sort(c(sample(year1, 50),
sample(year2, 50),
sample(year3, 50))),
value = runif(150)) |>
mutate(year = year(date))
dat
#> # A tibble: 150 × 3
#> date value year
#> <date> <dbl> <dbl>
#> 1 2019-03-07 0.265 2019
#> 2 2019-03-14 0.565 2019
#> 3 2019-03-23 0.913 2019
#> 4 2019-03-26 0.902 2019
#> 5 2019-04-01 0.274 2019
#> 6 2019-04-12 0.321 2019
#> 7 2019-04-19 0.986 2019
#> 8 2019-04-22 0.620 2019
#> 9 2019-05-11 0.937 2019
#> 10 2019-05-13 0.467 2019
#> # … with 140 more rows
I want to plot the lines such that the x-axis represents 1 year, and there is a separate line for each year. If I just put the date on the x-axis, each year shows up sequentially, as we would expect, but there are basically 3 years on the x-axis.
ggplot(dat, aes(x = date, y = value)) +
geom_line(aes(color = factor(year))) +
scale_x_date(breaks = "3 months", date_labels = "%B")
I have tried to use facets as a work around. This one has a similar problem of have three years on the x-axis.
ggplot(dat, aes(x = date, y = value)) +
facet_wrap(~year, ncol = 1) +
geom_line(aes(color = factor(year))) +
scale_x_date(breaks = "3 months", date_labels = "%B")
I can free the scales, but then the axes don’t line up, so it’s hard to compare across years.
ggplot(dat, aes(x = date, y = value)) +
facet_wrap(~year, ncol = 1, scales = "free_x") +
geom_line(aes(color = factor(year))) +
scale_x_date(breaks = "2 months", date_labels = "%B")
To illustrate what I’m looking for, I can force all dates to have the year 2020
.
This results in the plot I want, but it seems like there must be a better way than changing all of the dates?
year(dat$date) <- 2020
ggplot(dat, aes(x = date, y = value)) +
facet_wrap(~year, ncol = 1) +
geom_line(aes(color = factor(year))) +
scale_x_date(breaks = "2 months", date_labels = "%B")
Created on 2021-08-03 by the reprex package (v2.0.0)
Session infosessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.1.0 (2021-05-18)
#> os macOS Big Sur 10.16
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Chicago
#> date 2021-08-03
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
#> backports 1.2.1 2020-12-09 [1] standard (@1.2.1)
#> broom 0.7.9 2021-07-27 [1] CRAN (R 4.1.0)
#> cellranger 1.1.0 2016-07-27 [1] standard (@1.1.0)
#> cli 3.0.1 2021-07-17 [1] CRAN (R 4.1.0)
#> colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)
#> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0)
#> curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.0)
#> DBI 1.1.1 2021-01-15 [1] standard (@1.1.1)
#> dbplyr 2.1.1 2021-04-06 [1] standard (@2.1.1)
#> digest 0.6.27 2020-10-24 [1] CRAN (R 4.1.0)
#> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
#> evaluate 0.14 2019-05-28 [1] standard (@0.14)
#> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0)
#> farver 2.1.0 2021-02-28 [1] standard (@2.1.0)
#> forcats * 0.5.1 2021-01-27 [1] standard (@0.5.1)
#> fs 1.5.0 2020-07-31 [1] standard (@1.5.0)
#> generics 0.1.0 2020-10-31 [1] standard (@0.1.0)
#> ggplot2 * 3.3.5.9000 2021-07-18 [1] Github (tidyverse/ggplot2@13c0730)
#> glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0)
#> gtable 0.3.0 2019-03-25 [1] standard (@0.3.0)
#> haven 2.4.1 2021-04-23 [1] standard (@2.4.1)
#> highr 0.9 2021-04-16 [1] standard (@0.9)
#> hms 1.1.0 2021-05-17 [1] CRAN (R 4.1.0)
#> htmltools 0.5.1.1 2021-01-22 [1] standard (@0.5.1.1)
#> httr 1.4.2 2020-07-20 [1] standard (@1.4.2)
#> jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.0)
#> knitr 1.33 2021-04-24 [1] standard (@1.33)
#> labeling 0.4.2 2020-10-20 [1] standard (@0.4.2)
#> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.1.0)
#> lubridate * 1.7.10 2021-02-26 [1] standard (@1.7.10)
#> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)
#> mime 0.11 2021-06-23 [1] CRAN (R 4.1.0)
#> modelr 0.1.8 2020-05-19 [1] standard (@0.1.8)
#> munsell 0.5.0 2018-06-12 [1] standard (@0.5.0)
#> pillar 1.6.1 2021-05-16 [1] CRAN (R 4.1.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0)
#> purrr * 0.3.4 2020-04-17 [1] standard (@0.3.4)
#> R6 2.5.0 2020-10-28 [1] CRAN (R 4.1.0)
#> Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.0)
#> readr * 2.0.0 2021-07-20 [1] CRAN (R 4.1.0)
#> readxl 1.3.1 2019-03-13 [1] standard (@1.3.1)
#> reprex 2.0.0 2021-04-02 [1] standard (@2.0.0)
#> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.1.0)
#> rmarkdown 2.9.5 2021-07-30 [1] Github (rstudio/rmarkdown@bc936f7)
#> rstudioapi 0.13 2020-11-12 [1] standard (@0.13)
#> rvest 1.0.1 2021-07-26 [1] CRAN (R 4.1.0)
#> scales 1.1.1 2020-05-11 [1] standard (@1.1.1)
#> sessioninfo 1.1.1 2018-11-05 [1] standard (@1.1.1)
#> stringi 1.7.3 2021-07-16 [1] CRAN (R 4.1.0)
#> stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
#> styler 1.5.1 2021-07-13 [1] CRAN (R 4.1.0)
#> tibble * 3.1.3 2021-07-23 [1] CRAN (R 4.1.0)
#> tidyr * 1.1.3 2021-03-03 [1] standard (@1.1.3)
#> tidyselect 1.1.1 2021-04-30 [1] standard (@1.1.1)
#> tidyverse * 1.3.1 2021-04-15 [1] standard (@1.3.1)
#> tzdb 0.1.2 2021-07-20 [1] CRAN (R 4.1.0)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0)
#> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0)
#> withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)
#> xfun 0.24 2021-06-15 [1] standard (@0.24)
#> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.1.0)
#> yaml 2.2.1 2020-02-01 [1] standard (@2.2.1)
#>
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library