In the plot below the dates' zeros are not aligned with the other dates' digits when rotating the axis labels.
The plot itself is simple, a y on date line plot, the error comes from rotating the labels.
Is this a known bug? Are there any solutions?
library(ggplot2)
# create example data set
set.seed(2023)
df1 <- data.frame(
date = seq(as.Date("2022-01-01"), length.out = 12, by = "month"),
y = cumsum(rnorm(12))
)
# plot it
ggplot(df1,aes(date, y)) +
geom_line() +
scale_x_date(date_breaks = "3 months", date_labels = "%Y-%m") +
theme_classic() +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1))
Created on 2023-02-18 with reprex v2.0.2
Edit
If I open a graphics window before plotting the same misalignment occurs. So I don't think this has to do with RStudio, it seems the misalignment has to do with the screen display.
# explicitly open a graphics device
# could be X11(), the same artifact shows up
windows()
# plot code is the same
ggplot(df1,aes(date, y)) +
geom_line() +
scale_x_date(date_breaks = "3 months", date_labels = "%Y-%m") +
theme_classic() +
theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1))
R.version
#> _
#> platform x86_64-w64-mingw32
#> arch x86_64
#> os mingw32
#> crt ucrt
#> system x86_64, mingw32
#> status
#> major 4
#> minor 2.2
#> year 2022
#> month 10
#> day 31
#> svn rev 83211
#> language R
#> version.string R version 4.2.2 (2022-10-31 ucrt)
#> nickname Innocent and Trusting
packageVersion("ggplot2")
#> [1] '3.4.1'
Created on 2023-02-18 with reprex v2.0.2