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

hadley wickham h.wickham at gmail.com
Wed Nov 14 00:53:18 CET 2007


On Nov 13, 2007 4:30 PM, James D Forester <jdforest at uchicago.edu> wrote:
>
> Hi all,
>
> For various reasons, I need to use ggplot instead of qplot for a complex
> figure. Everything is working fine, except I cannot figure out how to rename
> the axis labels in ggplot. I have pasted a simple example below. Any ideas
> on what I am doing wrong?
>
> Thanks for your help.
>
> James
>
>
>
> library(ggplot2)
> ##create data
> mydat<-data.frame("site"=sample(c(0,1),100,replace=T),"response"=rnorm(100,5,2))
> mydat$predictor<-mydat$response*1.5+rnorm(100)
>
> ##qplot works fine
> qp<-qplot(predictor,response,data=mydat,xlab="Predictor
> Variable",ylab="Response Variable")
>
> ##However, trying this with ggplot, I cannot change the axis labels
> p<-ggplot(data=mydat,aes(shape=factor(site),x=predictor,y=response))
> p$xlabel="Predictor Variable"
> p$ylabel="Response Variable"
> p+geom_point()

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)

I keep meaning to come up with a more succinct way of setting the axis
labels, but I haven't managed it yet.

Hadley


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



More information about the R-help mailing list