ggplot2
ggplot2 copied to clipboard
geom_vline() does not extend the limits if the axis is a Date
Hi,
When using a date axis, geom_vline()
will only show if there is data nearby the intercept.
For numeric axes though (y in my example below), if the intercept is off-limit, the plot will extend to show the line, which is the expected behavior.
library(tidyverse)
library(lubridate)
set.seed(1234)
df = tibble(
x = as.Date(0:19, origin=ymd("2022-01-01")),
y=rnorm(20)
)
df %>%
ggplot(aes(x,y)) +
geom_point() +
geom_hline(yintercept=5) #will show (extends)
geom_vline(xintercept=ymd("2022-01-10")) + #will show (within limits)
geom_vline(xintercept=ymd("2022-03-01")) + #will not show (off-limit without extension)
Created on 2022-08-25 with reprex v2.0.2
Session info
sessionInfo()
#> R version 4.2.1 (2022-06-23 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 14393)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252
#> [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C
#> [5] LC_TIME=French_France.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] lubridate_1.8.0 forcats_0.5.2 stringr_1.4.1 dplyr_1.0.9
#> [5] purrr_0.3.4 readr_2.1.2 tidyr_1.2.0 tibble_3.1.8
#> [9] ggplot2_3.3.6 tidyverse_1.3.2
#>
#> loaded via a namespace (and not attached):
#> [1] assertthat_0.2.1 digest_0.6.29 utf8_1.2.2
#> [4] mime_0.12 R6_2.5.1 cellranger_1.1.0
#> [7] backports_1.4.1 reprex_2.0.2 evaluate_0.16
#> [10] httr_1.4.4 highr_0.9 pillar_1.8.1
#> [13] rlang_1.0.4 curl_4.3.2 googlesheets4_1.0.1
#> [16] readxl_1.4.1 rstudioapi_0.14 R.utils_2.12.0
#> [19] R.oo_1.25.0 rmarkdown_2.16 styler_1.7.0
#> [22] labeling_0.4.2 googledrive_2.0.0 munsell_0.5.0
#> [25] broom_1.0.0 compiler_4.2.1 modelr_0.1.9
#> [28] xfun_0.32 pkgconfig_2.0.3 htmltools_0.5.3
#> [31] tidyselect_1.1.2 fansi_1.0.3 crayon_1.5.1
#> [34] tzdb_0.3.0 dbplyr_2.2.1 withr_2.5.0
#> [37] R.methodsS3_1.8.2 grid_4.2.1 jsonlite_1.8.0
#> [40] gtable_0.3.0 lifecycle_1.0.1 DBI_1.1.3
#> [43] magrittr_2.0.3 scales_1.2.1 cli_3.3.0
#> [46] stringi_1.7.8 farver_2.1.1 fs_1.5.2
#> [49] xml2_1.3.3 ellipsis_0.3.2 generics_0.1.3
#> [52] vctrs_0.4.1 tools_4.2.1 R.cache_0.16.0
#> [55] glue_1.6.2 hms_1.1.2 fastmap_1.1.0
#> [58] yaml_2.3.5 colorspace_2.0-3 gargle_1.2.0
#> [61] rvest_1.0.3 knitr_1.40 haven_2.5.1
A workaround would be to add geom_blank(aes(x=ymd("2022-03-01")))
but it feels a bit unnecessary.