n <- 10
df <- tibble(
x = seq(as.Date("2020-01-01"), as.Date("2020-10-10"), length.out = n),
y = rnorm(n)
)
df
#> # A tibble: 10 × 2
#> x y
#> <date> <dbl>
#> 1 2020-01-01 0.973
#> 2 2020-02-01 -1.21
#> 3 2020-03-03 1.15
#> 4 2020-04-04 0.438
#> 5 2020-05-05 -0.322
#> 6 2020-06-06 -0.879
#> 7 2020-07-07 0.356
#> 8 2020-08-08 -1.16
#> 9 2020-09-08 -1.92
#> 10 2020-10-10 -1.04
p <- df %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
scale_x_date(date_breaks = "6 weeks", date_labels = "%B-%d")
p
Recently, I had to make a ggplot2
graphic with data that contained dates. Let’s create some sample data with dates.
My Linux system is configured in English, the dates are also displayed in English in the above graphic. If we want to use another language, we first have to edit the /etc/locale.gen
file and un-comment the line(s) for the language(s) we want to use. For this particular example, I wanted to display the dates in French.
sudo nano /etc/locale.gen
In this file, I un-commented the fr_CA.UTF-8 UTF-8
line. Once done, save the file. Then, we have to regenerate the localization files using the locale-gen
function.
sudo locale-gen fr_CA.UTF-8
Finally, within R, we can set the locale language with Sys.setlocale()
. If we call the plot again, the dates are now displayed in French as expected.
Sys.setlocale("LC_TIME", "fr_CA.UTF-8")
#> [1] "fr_CA.UTF-8"
p
Session info
#> ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.4.0 (2024-04-24)
#> os Ubuntu 24.04 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language en_CA:en
#> collate en_CA.UTF-8
#> ctype en_CA.UTF-8
#> tz America/Toronto
#> date 2024-05-02
#> pandoc 3.1.3 @ /usr/bin/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
#> ! package * version date (UTC) lib source
#> P cachem 1.0.8 2023-05-01 [?] RSPM (R 4.4.0)
#> P cli 3.6.2 2023-12-11 [?] RSPM (R 4.4.0)
#> P colorspace 2.1-0 2023-01-23 [?] RSPM (R 4.4.0)
#> P devtools 2.4.5 2022-10-11 [?] RSPM
#> P digest 0.6.35 2024-03-11 [?] RSPM (R 4.4.0)
#> P dplyr * 1.1.4 2023-11-17 [?] RSPM (R 4.4.0)
#> P ellipsis 0.3.2 2021-04-29 [?] RSPM (R 4.4.0)
#> P evaluate 0.23 2023-11-01 [?] RSPM (R 4.4.0)
#> P extrafont 0.19 2023-01-18 [?] RSPM (R 4.4.0)
#> P extrafontdb 1.0 2012-06-11 [?] RSPM (R 4.4.0)
#> P fansi 1.0.6 2023-12-08 [?] RSPM (R 4.4.0)
#> P farver 2.1.1 2022-07-06 [?] CRAN (R 4.4.0)
#> P fastmap 1.1.1 2023-02-24 [?] RSPM (R 4.4.0)
#> P forcats * 1.0.0 2023-01-29 [?] RSPM (R 4.4.0)
#> P fs 1.6.4 2024-04-25 [?] CRAN (R 4.4.0)
#> P generics 0.1.3 2022-07-05 [?] RSPM (R 4.4.0)
#> P ggplot2 * 3.5.1 2024-04-23 [?] RSPM (R 4.4.0)
#> P ggpmthemes * 0.0.2 2024-05-02 [?] Github (pmassicotte/ggpmthemes@993d61e)
#> P glue 1.7.0 2024-01-09 [?] RSPM (R 4.4.0)
#> P gtable 0.3.5 2024-04-22 [?] RSPM (R 4.4.0)
#> P hms 1.1.3 2023-03-21 [?] RSPM (R 4.4.0)
#> P htmltools 0.5.8.1 2024-04-04 [?] RSPM (R 4.4.0)
#> P htmlwidgets 1.6.4 2023-12-06 [?] RSPM
#> P httpuv 1.6.15 2024-03-26 [?] RSPM (R 4.4.0)
#> P jsonlite 1.8.8 2023-12-04 [?] RSPM (R 4.4.0)
#> P knitr 1.46 2024-04-06 [?] RSPM (R 4.4.0)
#> P labeling 0.4.3 2023-08-29 [?] CRAN (R 4.4.0)
#> P later 1.3.2 2023-12-06 [?] RSPM (R 4.4.0)
#> P lifecycle 1.0.4 2023-11-07 [?] RSPM (R 4.4.0)
#> P lubridate * 1.9.3 2023-09-27 [?] RSPM (R 4.4.0)
#> P magrittr 2.0.3 2022-03-30 [?] RSPM (R 4.4.0)
#> P memoise 2.0.1 2021-11-26 [?] RSPM (R 4.4.0)
#> P mime 0.12 2021-09-28 [?] RSPM (R 4.4.0)
#> P miniUI 0.1.1.1 2018-05-18 [?] RSPM (R 4.4.0)
#> P munsell 0.5.1 2024-04-01 [?] CRAN (R 4.4.0)
#> P pillar 1.9.0 2023-03-22 [?] RSPM (R 4.4.0)
#> P pkgbuild 1.4.4 2024-03-17 [?] RSPM (R 4.4.0)
#> P pkgconfig 2.0.3 2019-09-22 [?] RSPM (R 4.4.0)
#> P pkgload 1.3.4 2024-01-16 [?] RSPM
#> P processx 3.8.4 2024-03-16 [?] RSPM (R 4.4.0)
#> P profvis 0.3.8 2023-05-02 [?] RSPM
#> P promises 1.3.0 2024-04-05 [?] RSPM (R 4.4.0)
#> P ps 1.7.6 2024-01-18 [?] RSPM (R 4.4.0)
#> P purrr * 1.0.2 2023-08-10 [?] RSPM (R 4.4.0)
#> P quarto * 1.4 2024-03-06 [?] RSPM
#> P R.cache 0.16.0 2022-07-21 [?] RSPM
#> P R.methodsS3 1.8.2 2022-06-13 [?] RSPM
#> P R.oo 1.26.0 2024-01-24 [?] RSPM
#> P R.utils 2.12.3 2023-11-18 [?] RSPM
#> P R6 2.5.1 2021-08-19 [?] RSPM (R 4.4.0)
#> P Rcpp 1.0.12 2024-01-09 [?] RSPM (R 4.4.0)
#> P readr * 2.1.5 2024-01-10 [?] RSPM (R 4.4.0)
#> P remotes 2.5.0 2024-03-17 [?] RSPM
#> P renv 1.0.7 2024-04-11 [?] RSPM (R 4.4.0)
#> P rlang 1.1.3 2024-01-10 [?] RSPM (R 4.4.0)
#> P rmarkdown 2.26 2024-03-05 [?] RSPM (R 4.4.0)
#> P rstudioapi 0.16.0 2024-03-24 [?] RSPM
#> P Rttf2pt1 1.3.12 2023-01-22 [?] RSPM (R 4.4.0)
#> P scales 1.3.0 2023-11-28 [?] CRAN (R 4.4.0)
#> P sessioninfo 1.2.2 2021-12-06 [?] RSPM
#> P shiny 1.8.1.1 2024-04-02 [?] RSPM (R 4.4.0)
#> P stringi 1.8.3 2023-12-11 [?] RSPM (R 4.4.0)
#> P stringr * 1.5.1 2023-11-14 [?] RSPM (R 4.4.0)
#> P styler * 1.10.3 2024-04-07 [?] RSPM
#> P tibble * 3.2.1 2023-03-20 [?] RSPM (R 4.4.0)
#> P tidyr * 1.3.1 2024-01-24 [?] RSPM (R 4.4.0)
#> P tidyselect 1.2.1 2024-03-11 [?] RSPM (R 4.4.0)
#> P tidyverse * 2.0.0 2023-02-22 [?] RSPM (R 4.4.0)
#> P timechange 0.3.0 2024-01-18 [?] RSPM (R 4.4.0)
#> P tzdb 0.4.0 2023-05-12 [?] RSPM (R 4.4.0)
#> P urlchecker 1.0.1 2021-11-30 [?] RSPM
#> P usethis 2.2.3 2024-02-19 [?] RSPM
#> P utf8 1.2.4 2023-10-22 [?] RSPM (R 4.4.0)
#> P vctrs 0.6.5 2023-12-01 [?] RSPM (R 4.4.0)
#> P withr 3.0.0 2024-01-16 [?] RSPM (R 4.4.0)
#> P xfun 0.43 2024-03-25 [?] RSPM (R 4.4.0)
#> P xtable 1.8-4 2019-04-21 [?] RSPM (R 4.4.0)
#> P yaml 2.3.8 2023-12-11 [?] RSPM (R 4.4.0)
#>
#> [1] /tmp/Rtmp0lAB2f/renv-use-libpath-2e3a875de6e723
#> [2] /home/filoche/.cache/R/renv/sandbox/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu/a71ef467
#>
#> P ── Loaded and on-disk path mismatch.
#>
#> ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────