[R] Putting names on a ggplot

John Kane jrkrideau at yahoo.ca
Mon Oct 19 00:57:57 CEST 2009



--- On Sun, 10/18/09, ml at z107.de <ml at z107.de> wrote:

> From: ml at z107.de <ml at z107.de>
> Subject: Re: [R] Putting names on a ggplot
> To: "John Kane" <jrkrideau at yahoo.ca>
> Cc: "R R-help" <r-help at stat.math.ethz.ch>
> Received: Sunday, October 18, 2009, 6:05 PM
> hello,
> 
> On Sun, Oct 18, 2009 at 08:29:19AM -0700, John Kane wrote:
> > Thanks Stefan, the annotate approach works
> beautifully.  I had not got
> > that far in Hadley's book apparently :(
> > 
> > I'm not convinced though that the explaination
> > 
> > > you shouldn't use aes in this case since
> nampost,
> > > temprange, ... are not
> > > part of the dataframe year.
> > 
> > makes sense since it seems to work in this case unless
> I am missing
> > something obvious. Mind you I'm good at missing the
> obvious especially
> > in ggplot.
> 
> 
> hmm, I thought that aes() is used to map variables from the
> used dataset
> (data=...) to parts of the plot. But obviously this is
> wrong, since your
> example below works.
> 
> 
> In my (maybe out of date) pdf version of Hadley's book an
> page 46 it
> says:
> 
> |Any variable in an aes() specification must be contained
> inside the
> |plot or
> |layer data. This is one of the ways in which ggplot2
> objects are
> |guaranteed
> |to be entirely self-contained, so that they can be stored
> and
> |re-used.

I just realized I deleted Hadley's pdf so I cannot check this but it 
sounds right.

> 
> so, maybe the "problem" with your version is, you map
> variables of
> your environment (namposts, trange) to aesthetic, which
> works fine in
> your current environment, but fails if you save the plot
> and load it in
> another environmento.

> 
> | Error in eval(expr, envir, enclos) : object 'namposts'
> not found
> 
> 
> If you don't use save() on plots (I do not) this maybe not
> a problem for
> you.
> 

That's interesting. We're getting different error messages. I get

> p
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 12, 366

which makes your first idea seem more reasonable but I don't see why my other example would work if it was a data.frame problem :(


Thanks for following up on this.  

I just installed 2.9.2 yesterday so I think I'm up to date on ggplot2 .



> > 
> > Example
> >
> =========================================================================
> > library(ggplot2)
> > 
> > month  <-  rep(month.abb[1:12], each=30)
> >        days  <- rep(1:30,
> 12)
> >        temps  <- 
> rnorm(length(month), mean=25,sd=8)
> > duration  <- 1:length(month)
> > 
> > timedata <- data.frame(month, days, temps,
> duration)
> > head(timedata)
> > 
> > mbs  <- c(1,seq(30, 360,by=30))
> > namposts <- mbs[1:12]
> > mlabs <- month.name[1:12]
> > trange <- range(timedata$temps)
> > drange <- range(duration)
> > 
> >              
> > p  <- ggplot(timedata, aes(duration, temps,
> colour=month)) + geom_line() +
> >        opts(legend.position =
> "none", title="Yearly temperatures",
> >        axis.text.x =
> theme_blank(), axis.ticks = theme_blank())  
> > 
> > 
> > p  <- p + geom_vline(xintercept= mbs) + 
> >       ylab("Temperature (C)")
> + xlab("Daily Temperatures") +
> >       geom_text(aes(x =
> namposts+2.5, y = trange[2], label = mlabs),
> >       data = timedata, size =
> 2.5, colour='black', hjust = 0, vjust = 0)
> > p
> > 
> > 
> >
> =========================================================================
> > --- On Sat, 10/17/09, ml at z107.de <ml at z107.de> wrote:
> > 
> > > From: ml at z107.de <ml at z107.de>
> > > Subject: Re: [R] Putting names on a ggplot
> > > To: "John Kane" <jrkrideau at yahoo.ca>
> > > Cc: "R R-help" <r-help at stat.math.ethz.ch>
> > > Received: Saturday, October 17, 2009, 5:53 PM
> > > hi,
> > > 
> > > On Sat, Oct 17, 2009 at 02:04:43PM -0700, John
> Kane wrote:
> > > > Putting names on a ggplot
> > > > 
> > > > p <- p + geom_text(aes(x = namposts +
> 2.5, y =
> > > temprange[2], label =
> > > > mlabs),
> > > >       data = year, size =
> > > 2.5, colour='black', hjust = 0, vjust = 0)
> > > > 
> > > 
> > > you shouldn't use aes in this case since
> nampost,
> > > temprange, ... are not
> > > part of the dataframe year.
> > > 
> > > It should also work with geom_text i guess, but I
> prefere
> > > annotate for
> > > thinks like that:
> > > 
> > > p + annotate("text",x=namposts+2.5, y =
> temprange[2],
> > > label=
> > >     mlabs,size=2.5,colour='black', hjust = 0,
> > > vjust = 0)
> > > 
> > > regards,
> > >     stefan
> > > 
> > 
> > 
> >   
>    __________________________________________________________________
> > Yahoo! Canada Toolbar: Search from anywhere on the
> web, and bookmark your favourite sites. Download it now
> > http://ca.toolbar.yahoo.com.
> > 
> 


      __________________________________________________________________
Ask a question on any topic and get answers from real people. Go to Yahoo! Answe



More information about the R-help mailing list