Misaligned zeros in rotated axis date labels

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

Good reprex. Here's what I get with it on an M1, so I'm guessing it has to do with how your graphics dev() is setup. Try saving with

ggsave(file = "Rplot.png")

and see if it's just an artifact of how RStudio is presenting it in your setup?

image

1 Like

Thanks for the reply, ggsave() also solves the problem for me. However, now I don't think this is unique to RStudio, when I try to open a graphics device before plotting the same artifact shows up. I will edit the question with an example of this and with the session info.

Sorry to be unclear. The image was copied from the GUI, not ggsaved

This topic was automatically closed 7 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.