[R] ggplot2: changing axis labels in ggplot()

hadley wickham h.wickham at gmail.com
Wed Nov 14 02:30:11 CET 2007


On Nov 13, 2007 7:23 PM, James D Forester <jdforest at uchicago.edu> wrote:
>
>
> hadley wrote:
> >
> >
> > That's from a previous version, now you need to do:
> >
> > p + scale_x_continuous("Predictor") + scale_y_continuous("Response")
> >
> > (or scale_x_discrete if discrete)
> >
> >
>
> Thanks Hadley -- that worked fine. However, I just noticed that when I try
> to add an abline to this plot a strange error appears. First, I update the
> abline function based on your advice
> http://www.nabble.com/ggplot2-geom_abline-slope-not-working--tf4770727.html#a13646422
> here .
>
> Then I try to add a line to the example I used above:
>
> p<-ggplot(data=mydat,aes(shape=factor(site),x=predictor,y=response))
>
> p+geom_point()+
>   scale_x_continuous("Predictor Variable")+
>   scale_y_continuous("Response Variable") +
>   geom_abline(slope=0,intercept=5)
>
>
> The error I get is:
>
> Error in typeof(x) : object "site" not found
>
>
> Is this a bug or a syntax error?

I keep going back and forth on whether this should be an error or not.
 There's a particular problem here with the way geom_abline works
behind the scenes, but there's a more general problem of when you have
layer with a different data source that doesn't have the variables you
need.  For your case, the easiest fix is probably to only specify the
colour mapping in the point layer:

 + geom_point(aes(colour = site))

Hadley



-- 
http://had.co.nz/



More information about the R-help mailing list