0

The following code works like a charm:

foo = data.frame(x=c(1,2,3),
                 y=c(4,5,6),
                 lt=c('dotted', 'dashed', 'dotdash'))
ggplot(foo, aes(x,y)) + geom_point() +
  geom_vline(aes(xintercept=x, linetype=lt))

enter image description here

The following code drops linetype. Why?

ggplot(foo, aes(x,y)) + geom_point() +
  geom_vline(xintercept=3, aes(linetype=lt)) +
  facet_wrap(~lt)

enter image description here

I am using ggplot 0.9.3.1 in Rstudio.

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
dfrankow
  • 20,191
  • 41
  • 152
  • 214

1 Answers1

2

I can confirm your problem and also that Sandy's comment fixes it for me, using ggplot2 1.0 in R 3.1.2:

geom_vline(aes(xintercept = 3, linetype = lt))

However I still think this is a bug in (maybe just the documentation for) geom_vline. I think xintercept should be listed as a required aesthetic with a default value of 0. Notably, the code below "works" -- the lines have the correct hashing at the default x location of 0:

ggplot(foo, aes(x,y)) + 
   geom_point() +
   geom_vline(aes(linetype=lt)) +
   facet_wrap(~lt)

So the problem seems to be that when required aesthetics are supplied outside of aes(), the arguments inside of aes() are ignored. This is different than other functions. For example,

ggplot(foo) + geom_point(x=1, aes(y=y))

gives an error but

ggplot(foo) + geom_point(aes(x=1, y=y))

does not. As far as I'm concerned, ideal behavior would be for geom_vline() to behave the same way as geom_point() in this regard.

Curt F.
  • 4,690
  • 2
  • 22
  • 39