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

geom_vline should work with non-numeric scales #84

Closed
hadley opened this issue Mar 17, 2010 · 5 comments
Closed

geom_vline should work with non-numeric scales #84

hadley opened this issue Mar 17, 2010 · 5 comments
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@hadley
Copy link
Member

hadley commented Mar 17, 2010

e.g. date and character

@HarlanH
Copy link

HarlanH commented Sep 29, 2011

Here here!

For dates, the workaround is to wrap the Date with as.numeric, ala: geom_vline(xintercept=as.numeric(Sys.Date))

@hadley
Copy link
Member Author

hadley commented Dec 2, 2011

@HarlanH would you mind contributing a couple of reproducible examples?

@BrianDiggs
Copy link
Contributor

DF <- data.frame(dt = as.Date("2000-01-01")+0:20, val=0:20)

ggplot(DF, aes(x=dt, y=val)) +
  geom_point() +
  geom_vline(xintercept=as.Date("2000-01-10"))

ggplot(DF, aes(x=dt, y=val)) +
  geom_point() +
  geom_vline(xintercept=as.numeric(as.Date("2000-01-10")))

The first does not have a vertical line; the second does, at the expected location. This was run with 0.8.9. This is not up to a testable example, but it is a start.

If you are going to handle non-numeric scales, then this should work as well

ggplot(iris, aes(x=Species, y=Sepal.Length)) +
  geom_point() +
  geom_vline(xintercept="setosa")

@davidcoallier
Copy link

I know this issue is more than 2 years old but I figured I'd add a comment.

When using the following code (DF from above in this case):

ggplot(DF, aes(x=dt, y=val)) +
    geom_point() +
    geom_vline(
        xintercept = as.numeric(as.Date("2001-01-10")))

One has to be careful that the values used for the x-axis date-values are formatted using the same standard.

For instance, if your data-frame DF contains dates formatted using as.POSIXct instead of as.Date your plot will look quite weird. e.g.:

DF <- data.frame(dt = as.POSIXct("2000-01-01")+0:20, val=0:20)

ggplot(DF, aes(x=dt, y=val)) +
  geom_point() +
  geom_vline(xintercept=as.Date("2000-01-10"))

ggplot(DF, aes(x=dt, y=val)) +
  geom_point() +
  geom_vline(xintercept=as.numeric(as.Date("2000-01-10")))

For instance, the as.numeric values for the date "2000-01-10" is the following:

  • as.POSIXct("2000-01-10"): 979084800
  • as.Date("2000-01-10"): 11332

This example is trivial since we know that DF has been defined with as.Date but I've encountered larger codebases where the likes of :

format(as.POSIXct("2001-01-10"), format="%Y-%m-%d")

made it a lot more annoying to process when generating plots.

It is a really simple tip however if you are running larger sequential list of intercepts and your dataset is large, your memory will burst in tears straight through the roof.

@hadley
Copy link
Member Author

hadley commented Feb 24, 2014

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

@hadley hadley closed this as completed Feb 24, 2014
@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.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants