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

annotate("segment", ...) doesn't work with dates on x-axis #859

Closed
vkryukov opened this issue Sep 16, 2013 · 4 comments
Closed

annotate("segment", ...) doesn't work with dates on x-axis #859

vkryukov opened this issue Sep 16, 2013 · 4 comments

Comments

@vkryukov
Copy link

@vkryukov vkryukov commented Sep 16, 2013

How to reproduce:

library(data.table)
library(lubridate)
library(ggplot2)

# Prepare some random data
set.seed(1234)
dt <- data.table(x = rnorm(365*5), d = seq(ymd(20130101), ymd(20131231), by = 86400))
dt.m <- dt[, list(total = sum(x)), by = list(month = floor_date(d, "month"))]
# Create a basic scatterplot chart
p <- qplot(month, total, data = dt.m)

This works:

# Both of these work as expected and produce the same result
p + geom_segment(x = as.numeric(ymd(20130401)), xend = as.numeric(ymd(20130701)), 
  y = -10, yend = 10)
p + geom_segment(aes(x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10))

but this doesn't:

> p + annotate("segment", x = as.numeric(ymd(20130401)), xend = as.numeric(ymd(20130701)), 
  y = -10, yend = 10)
Error: Invalid input: time_trans works with objects of class POSIXct only

> p + annotate("segment", x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10)
Error in Ops.POSIXt((x - from[1]), diff(from)) : 
  '/' not defined for "POSIXt" objects 

> p + annotate("segment", aes(x = ymd(20130401), xend = ymd(20130701), 
  y = -10, yend = 10))
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""uneval"" to a data.frame

I have also posted a question to SO - there may be some additional details there. http://stackoverflow.com/questions/18796386/how-to-use-ggplot2s-annotate-with-dates-in-x-axis

@BrianDiggs
Copy link
Contributor

@BrianDiggs BrianDiggs commented Sep 16, 2013

A simplified test case which eliminates the dependence on data.table and lubridate (just to make sure that neither is the source of the problem):

dt <- 
structure(list(month = structure(c(1356998400, 1359676800, 1362096000, 
1364774400, 1367366400, 1370044800, 1372636800, 1375315200, 1377993600, 
1380585600, 1383264000, 1385856000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), total = c(-5.4586134857604, -13.7043187714622, 3.25254944094792, 
-10.3260511491301, 6.44178978776937, 2.46127352381964, -1.98673390397442, 
-16.3227453308624, 11.7148615457185, -2.23934628094151, 5.5084335509504, 
-10.7700077344507)), row.names = c(NA, -12L), class = "data.frame", .Names = c("month", 
"total"))

p <- qplot(month, total, data = dt)

p + geom_segment(x = as.numeric(as.POSIXct("2013-04-01")),
                 xend = as.numeric(as.POSIXct("2013-07-01")),
                 y = -10,
                 yend = 10)

p + geom_segment(aes(x = as.POSIXct("2013-04-01"),
                     xend = as.POSIXct("2013-07-01"),
                     y = -10,
                     yend = 10))

p + annotate("segment",
             x = as.numeric(as.POSIXct("2013-04-01")),
             xend = as.numeric(as.POSIXct("2013-07-01")),
             y = -10,
             yend = 10)
# Error: Invalid input: time_trans works with objects of class POSIXct only

p + annotate("segment",
             x = as.POSIXct("2013-04-01"),
             xend = as.POSIXct("2013-07-01"),
             y = -10,
             yend = 10)
# Error in Ops.POSIXt((x - from[1]), diff(from)) : 
#   '/' not defined for "POSIXt" objects

I did not include the version with annotate and aes because that is not how `annotate is meant to be called, so an error there is not surprising.

BrianDiggs added a commit to BrianDiggs/ggplot2 that referenced this issue Sep 16, 2013
@hadley
Copy link
Member

@hadley hadley commented Feb 24, 2014

This sounds like a great feature/horrible bug, but unfortunately we don't currently have the development bandwidth to support it/fix it. If you'd like to submit a pull request that implements this feature/fixes this bug, please follow the instructions in the development vignette.

@hadley hadley closed this Feb 24, 2014
BrianDiggs added a commit to BrianDiggs/ggplot2 that referenced this issue Apr 24, 2014
Fixes bug where `xend` and `yend` were not passed along
as coordinates for transformation within `annotate`. This
led to the end of segments not appearing in the correct
location when a transformation was involved with one of the
coordinate scales.
@ghost
Copy link

@ghost ghost commented Aug 12, 2014

Tried to install the latest development version from github install_github("hadley/ggplot2"), but this problem still persists.

@BrianDiggs
Copy link
Contributor

@BrianDiggs BrianDiggs commented Aug 26, 2014

The patch for this was resubmitted as #950, but that has not been merged into the master branch yet.

hadley added a commit that referenced this issue Jun 23, 2015
Fixes #859, update of #860
@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants