Skip to content
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

scale_x_date passes NAs to the label function #2182

Closed
ggrothendieck opened this issue Jun 26, 2017 · 3 comments
Closed

scale_x_date passes NAs to the label function #2182

ggrothendieck opened this issue Jun 26, 2017 · 3 comments
Labels
bug an unexpected problem or unintended behavior scales 🐍

Comments

@ggrothendieck
Copy link

ggrothendieck commented Jun 26, 2017

When using scale_x_date(..., label = fun) it can be that NAs are passed to fun which adds undesirable complexity to the definition of the function.

See this example:
https://stackoverflow.com/questions/44616530/prevent-showing-the-year-several-times-unnecessarily-with-time-series/44751923#44751923

where this line had to be added to avoid errors even though the NAs do not actually correspond to breaks:

b[is.na(b)] <- Sys.Date()

Any date could have been used in place of Sys.Date() since it seems that the date itself is not actually used if the passed break is NA.

@karawoo
Copy link
Member

karawoo commented Jul 10, 2017

Could you please provide a minimal reproducible example using the reprex package? I'm having trouble reproducing the example from that SO link.

@karawoo karawoo added the reprex needs a minimal reproducible example label Jul 10, 2017
@ggrothendieck
Copy link
Author

ggrothendieck commented Jul 11, 2017

This fails with an error message.; howver, if we uncomment the line containing Sys.Date() then it works. The problem seems to be that ggplot2 passes a vector containing some NA components to the lab function and one must provide a valid label for them even though ggplot2 never uses those labels. ggplot2 should not pass these NA values to the label function.

    library(ggplot2)
    library(scales)

    # test input
    df <- data.frame(Date = as.Date(c("1719-08-10", "1720-11-25")), value = 0:1)

    lab <- function(b) {
      print(b)  # note NA components of b
      # b[is.na(b)] <- Sys.Date()  # uncomment this line and it will work
      format(b, ifelse(as.POSIXlt(b)$mon == 0, "%Y", "%b"))
    }

    ggplot(df, aes(Date, value)) + 
       geom_line() +
       scale_x_date(date_breaks = "month", labels = lab)

The output of the print statement (in the lab function) upon running the above code is shown below. Note the NA values that were passed to lab unnecessarily complicating the lab function's code.

 [1] NA           "1719-08-01" "1719-09-01" "1719-10-01" "1719-11-01"
 [6] "1719-12-01" "1720-01-01" "1720-02-01" "1720-03-01" "1720-04-01"
[11] "1720-05-01" "1720-06-01" "1720-07-01" "1720-08-01" "1720-09-01"
[16] "1720-10-01" "1720-11-01" "1720-12-01" NA 

@hadley hadley added bug an unexpected problem or unintended behavior scales 🐍 and removed reprex needs a minimal reproducible example labels Jul 13, 2017
@hadley
Copy link
Member

hadley commented Oct 31, 2017

The root cause is that the breaks algorithm generates breaks that are outside of the limits, and these get converted to NAs. It would be possible to fix this problem but it would require modifying code that I no longer understand and is probably not terribly well tested. Given that there's an existing work around (make sure your labelling function can handle NAs), unfortunately I don't think it's worthwhile to try and fix this bug.

@hadley hadley closed this as completed Oct 31, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

No branches or pull requests

3 participants