Skip to content

Error with date scale and Infinity (geom_segment) #4308

Closed

Description

@DanChaltiel

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.

Activity

  1. teunbrand commented on Jan 5, 2021

    @teunbrand
    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)

  2. DanChaltiel commented on Jan 5, 2021

    @DanChaltiel
    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.

  3. thomasp85 commented on Mar 24, 2021

    @thomasp85
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions