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

Error with date scale and Infinity (geom_segment) #4308

Closed
DanChaltiel opened this issue Jan 5, 2021 · 3 comments
Closed

Error with date scale and Infinity (geom_segment) #4308

DanChaltiel opened this issue Jan 5, 2021 · 3 comments

Comments

@DanChaltiel
Copy link

To emphasize a certain point, you might want to actually draw its coordinates using geom_segment().

When using geom_segment(), you can use Inf if the mapping so that the segment will start on the edge of the plot.

However, if you use Inf like this on an axis of class Date, it will throw an error:

Error: Invalid input: date_trans works with objects of class Date only

Here is an example with a simple workaround:

library(tidyverse)
library(lubridate)
set.seed(1234)
last_month <- ymd("2020-12-01") - 29:0

df <- data.frame(
  date = last_month,
  price = runif(30)
)
max_y = max(df$price)
max_x = df$date[df$price==max(df$price)][1]

base <- ggplot(df, aes(date, price)) +
  geom_line(color="red")

base+
  geom_segment(aes(x=max_x, xend=max_x, y=-Inf, yend=max_y)) +
  geom_segment(aes(x=-Inf, xend=max_x, y=max_y, yend=max_y))
#> Error: Invalid input: date_trans works with objects of class Date only

base+
  scale_x_continuous(breaks=ymd("2020-11-01", "2020-11-10", "2020-11-20", "2020-12-01")) +
  geom_segment(aes(x=max_x, xend=max_x, y=-Inf, yend=max_y)) +
  geom_segment(aes(x=-Inf, xend=max_x, y=max_y, yend=max_y))

Created on 2021-01-05 by the reprex package (v0.3.0)

As you can see, adding a scale_x_continuous() call solves this issue without breaking the date labels so maybe this error is not mandatory.

@teunbrand
Copy link
Collaborator

It works fine if you re-class the -Inf to a Date class, but it is a bit of an inconvenient workaround.
Some time ago I opened an issue in the scales package suggesting that the date transformation be allowed some more flexibility around numeric input, which might help these kind of issues.

library(tidyverse)
library(lubridate)
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
set.seed(1234)
last_month <- ymd("2020-12-01") - 29:0

df <- data.frame(
  date = last_month,
  price = runif(30)
)
max_y = max(df$price)
max_x = df$date[df$price==max(df$price)][1]

base <- ggplot(df, aes(date, price)) +
  geom_line(color="red")

base+
  geom_segment(aes(x=max_x, xend=max_x, 
                   y=structure(-Inf, class = "Date"), yend=max_y)) +
  geom_segment(aes(x=structure(-Inf, class = "Date"), xend=max_x, 
                   y=max_y, yend=max_y))

Created on 2021-01-05 by the reprex package (v0.3.0)

@DanChaltiel
Copy link
Author

Nice workaround as well!
This is actually much better than my way of doing it, as you can declare date_minf = structure(-Inf, class = "Date") and use it freely in the mapping.

@thomasp85
Copy link
Member

I agree that this is more of a scales issue, so I'm closing it here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants