[R] Putting names on a ggplot
John Kane
jrkrideau at yahoo.ca
Sun Oct 18 17:29:19 CEST 2009
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.
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