0

I might be missing something very silly, can't get it to work. For example:

require(ggplot2)
#sample data
dat <- data.frame(x=1:5,
                  y=1:5,
                  z=1:5)
dat1 <- data.frame(x=1:5,
                   b=c(2,3,3,4,4))

Following works:

#This works:
ggplot(data=dat,aes(x=x,y=y,colour=z)) +
  geom_point()

#This works, too:
ggplot(data=dat1,aes(x=x,y=b)) +
  geom_line()

When I try to plot them together, it can't find z:

Error in eval(expr, envir, enclos) : object 'z' not found

#This errors out
ggplot(data=dat,aes(x=x,y=y,colour=z)) +
  geom_point() +
  geom_line(data=dat1,aes(x=x,y=b))

EDIT:

Relevant post: add stripplot from different data.frame

Community
  • 1
  • 1
zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

1

For example:

ggplot() +
  geom_point(data=dat,aes(x=x,y=y,colour=z)) +
  geom_line(data=dat1,aes(x=x,y=b))
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • This gives an error: `Error: ggplot2 doesn't know how to deal with data of class uneval` – zx8754 Jun 13 '14 at 10:50
  • 1
    @zx8754 I correct it. I don't know why the other doesn’t' work. – agstudy Jun 13 '14 at 10:54
  • Thanks, this works. Also, found relevant post http://stackoverflow.com/questions/22844881/add-stripplot-from-different-data-frame Does this mean my post is a duplicate? Solution from the other post is about inheritance between layers. – zx8754 Jun 13 '14 at 10:59
  • @agstudy the first argument to `ggplot()` is data, you initially fed it a mapping. – baptiste Jun 13 '14 at 11:17
  • 1
    @baptiste I guess this alos. but `ggplot2` doc don't show the naming parameters...I should go in the code to see them. – agstudy Jun 13 '14 at 11:32
  • ggplot is a generic, with hidden default methods. Still, the help page `?ggplot` and `formals(ggplot2:::ggplot.data.frame)` show that the first argument is "data". – baptiste Jun 13 '14 at 14:33
  • @baptiste Thanks. Maybe they should add this to the documentation. – agstudy Jun 13 '14 at 14:36
  • @agstudy I'm confused, isn't it already [there?](http://docs.ggplot2.org/current/ggplot.html) – baptiste Jun 13 '14 at 14:40
  • @baptiste I am talking about the `...` parameters. for example in the following example I need to set `mapping` then data... – agstudy Jun 13 '14 at 14:42
  • @agstudy the order is different for geoms (layers), it's probably a source of confusion, yes, and you're correct in that `?layer` says nothing of the sort. – baptiste Jun 13 '14 at 14:52