ggplot2::scale_x_date limits = NA doesn't recognize a date variable

I am plotting election data and wanted the x axis to represent years in order from 1994 to 2019.
I tried applying limits = NA in scale_x_date from ggplot2 but it gives an error message:

Error: Invalid input: date_trans works with objects of class Date only

Reprex below.

suppressPackageStartupMessages({
  library(dplyr)
  library(lubridate)
  library(ggplot2)
})

df <- 
tibble::tribble(
  ~region, ~year,    ~ballot, ~party,    ~votes, ~seats, ~pct_votes,
  "ZA", 1994L, "National",  "ANC", 12237655L,   252L,      0.626,
  "ZA", 1999L, "National",  "ANC", 10601330L,   266L,      0.664,
  "ZA", 2004L, "National",  "ANC", 10880915L,   279L,      0.697,
  "ZA", 2009L, "National",  "ANC", 11650748L,   264L,      0.659,
  "ZA", 2014L, "National",  "ANC", 11436921L,   249L,      0.621,
  "ZA", 2019L, "National",  "ANC", 10026475L,   230L,      0.575
)

df |> 
  mutate(year = make_date(year = year, month = 04, day = 27)) |> 
  ggplot(aes(
    x = year, y = votes, fill = party, color = party, group = party)) +
  scale_x_date(limits = NA) +
  geom_col()
#> Error: Invalid input: date_trans works with objects of class Date only

sessionInfo()
#> R version 4.3.1 (2023-06-16 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 11 x64 (build 22621)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_South Africa.utf8  LC_CTYPE=English_South Africa.utf8   
#> [3] LC_MONETARY=English_South Africa.utf8 LC_NUMERIC=C                         
#> [5] LC_TIME=English_South Africa.utf8    
#> 
#> time zone: Africa/Johannesburg
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.4.3   lubridate_1.9.3 dplyr_1.1.3    
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.3       cli_3.6.1         knitr_1.44        rlang_1.1.1      
#>  [5] xfun_0.40         generics_0.1.3    glue_1.6.2        colorspace_2.1-0 
#>  [9] htmltools_0.5.6   scales_1.2.1      fansi_1.0.4       rmarkdown_2.24   
#> [13] grid_4.3.1        munsell_0.5.0     evaluate_0.21     tibble_3.2.1     
#> [17] fastmap_1.1.1     yaml_2.3.7        lifecycle_1.0.3   compiler_4.3.1   
#> [21] fs_1.6.3          timechange_0.2.0  pkgconfig_2.0.3   rstudioapi_0.15.0
#> [25] digest_0.6.33     R6_2.5.1          reprex_2.0.2      tidyselect_1.2.0 
#> [29] utf8_1.2.3        pillar_1.9.0      magrittr_2.0.3    gtable_0.3.4     
#> [33] tools_4.3.1       withr_2.5.1

Created on 2023-10-07 with reprex v2.0.2

Use the default, NULL, by simply omitting the argument. And create a Date class object to replace the typeof integer variable year

suppressPackageStartupMessages({
  library(ggplot2)
})

d <- 
  tibble::tribble(
    ~region, ~year,    ~ballot, ~party,    ~votes, ~seats, ~pct_votes,
    "ZA", 1994L, "National",  "ANC", 12237655L,   252L,      0.626,
    "ZA", 1999L, "National",  "ANC", 10601330L,   266L,      0.664,
    "ZA", 2004L, "National",  "ANC", 10880915L,   279L,      0.697,
    "ZA", 2009L, "National",  "ANC", 11650748L,   264L,      0.659,
    "ZA", 2014L, "National",  "ANC", 11436921L,   249L,      0.621,
    "ZA", 2019L, "National",  "ANC", 10026475L,   230L,      0.575
  )


d$date <- as.Date(paste(as.character(d$year), "-04-07", sep=""), format="%Y-%m-%d")

d |> ggplot(aes(
  x = date, y = votes, fill = party, color = party, group = party)) +
  scale_x_date() +
  geom_col() +
  theme_minimal()

image

Created on 2023-10-07 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.