-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Misaligned date axis for some custom breaks functions #3965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks, confirmed. Here's a bit different version of the reprex. Maybe some transformation or back-transformation of the Date scale doesn't handle the expansion properly...? library(ggplot2)
library(patchwork)
# In my locale, there's no "Monday" and "Friday" :P
Sys.setlocale(locale = "C")
#> [1] "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=ja_JP.UTF-8;LC_PAPER=ja_JP.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=ja_JP.UTF-8;LC_IDENTIFICATION=C"
set.seed(pi)
df <- data.frame(date = Sys.Date() - 0:29, price = runif(30))
p <- ggplot(df, aes(date, price)) + geom_line() + geom_point() + coord_fixed(ratio=5)
weekend_breaks_correct <- function(x) {
mondays <- scales::fullseq(x, "1 week")
fridays <- mondays - lubridate::days(3)
res <- sort(c(mondays, fridays))
print(res)
res
}
weekend_breaks_misaligned <- function(x){
x <- seq(x[1], x[2], by=1)
res <- x[weekdays(x) %in% c('Monday','Friday')]
print(res)
res
}
p1 <- p + scale_x_date(date_labels = "%a %b %d", breaks = weekend_breaks_misaligned, expand = c(0.05, 0.05))
p2 <- p + scale_x_date(date_labels = "%a %b %d", breaks = weekend_breaks_misaligned, expand = c(0, 0))
p1 / p2
Created on 2020-04-27 by the reprex package (v0.3.0) |
@yutannihilation's assumption is almost right. From Date object's documentation:
In above example breaks contain 0.5 fractional part in numeric presentation. That's why breaks moved a little bit to right side. In above example, first time fractional part appears after expanding limits. So, user's breaks function obtain transformed bad date objects and also return bad date objects. Reverse transformation of breaks is also doesn't check to non-zero fractional part. There are three solutions to this "bug":
|
Uh oh!
There was an error while loading. Please reload this page.
A custom breaks function on the date axis seem to mis-align the breaks and labels. Another does not. But corresponding date vector inputs for both functions work fine.
Not sure if this has already been reported - I've looked and found this issue, but it's old.
Reproducible example - note how the 30 March break shifts off the maximum data point it should align with.
The text was updated successfully, but these errors were encountered: