[R] Putting names on a ggplot

ml at z107.de ml at z107.de
Mon Oct 19 00:05:37 CEST 2009


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.

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.

regards,
 stefan



> 
> 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.
>




More information about the R-help mailing list