[R] ggplot 'annotate problem' again.

smu ml at z107.de
Mon Mar 1 08:16:58 CET 2010


Hi,

I suggest to use geom_text to add multiple labels. 
Put all the information needed by for your labels in a dataframe:

lab = data.frame(x=namposts+2.5, y = temprange[2], label= mlabs)

and add them to the plot:

p + geom_text(data=lab,aes(x,y,label=label), size=2.5,colour='black',
    hjust = 0, vjust = 0)


regards,
 Stefan



On Sun, Feb 28, 2010 at 01:25:15PM -0800, John Kane wrote:
> I had a problem annotating a graph last year ( see
> http://n4.nabble.com/Putting-names-on-a-ggplot-td907158.html#a907158
> for the discussion)
> 
> Stefan (smu) provided a solution using annotate().  However I
> apparently did not update the graph file and,now, when I go back to
> the thread and try to use Stefan's solution it does not seem to work
> although I am sure that it did then.  
> 
> The problem arises at:
>  
> > p + annotate("text", x=namposts+2.5, y = temprange[2], label= mlabs,
> > size=2.5, colour='black', hjust = 0, vjust = 0)
> 
> Error: When _setting_ aesthetics, they may only take one value.
> Problems: label
> 
> 
> I want the names of the months along the top of the graph (one month
> per division).It appears that I am  being told that the label can only
> have one value. mlabs contains the 12 months of the year If this is
> true is there another way to annotate the graph?
> 
> Unfortunately ?annotate only shows a one value entry.
> 
> Any suggestions would be welcome.
> 
> 
> 
> Code
> # Data is below code.
> #==============================================================
> library(ggplot2)
> 
> year[,5] <- 1:length(year[,1])
> names(year) <- c('month','days','year','temps', "duration")
> 
> month.days <- as.vector(table(year[,1]))
> months <- rep(month.name[1:12], month.days)
> month.counts <-  cumsum(as.vector( table(year[,1])))
> namposts <- c(1, month.counts[1:11])
> mlabs <- month.name[1:12]
> temprange <- range(year$temps, na.rm=TRUE)
> 
> p  <- ggplot(year, aes(duration, temps, colour=months)) + geom_line() +
>        opts(legend.position = "none", title="Yearly temperatures",
>        axis.text.x = theme_blank(), axis.ticks = theme_blank())
> p
> p  <- p + geom_vline(xintercept=month.counts) +
>       ylab("Temperature (C)") + xlab("Daily Temperatures")
> p
> p + annotate("text", x=namposts+2.5, y = temprange[2], label= mlabs, size=2.5,
>    colour='black', hjust = 0, vjust = 0)
>    
> 
> Data
> #=================================================================
> year <- structure(list(month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
> 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
> 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
> 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
> 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
> 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
> 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
> 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L,
> 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
> 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
> 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
> 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
> 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
> 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
> 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
> 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), days = c(1L, 2L,
> 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
> 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
> 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
> 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
> 26L, 27L, 28L, 29L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
> 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
> 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
> 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L,
> 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
> 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
> 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
> 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
> 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
> 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
> 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L,
> 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
> 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
> 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
> 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
> 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
> 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
> 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
> 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
> 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L,
> 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
> 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
> 31L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L,
> 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L), temps = c(-2.77777777777778,
> -13.3333333333333, -19.9444444444444, -8.44444444444444, -3.38888888888889,
> 2.16666666666667, 4.05555555555555, 6.94444444444444, 7, 0.88888888888889,
> -0.666666666666666, 0.611111111111112, -5.83333333333333, -4.5,
> -6.44444444444444, -7.66666666666667, -7.38888888888889, -2.55555555555556,
> -5.55555555555556, -14.5555555555556, -16.1111111111111, -6.11111111111111,
> -9, -15.6111111111111, -11.9444444444444, -7.83333333333333,
> -9.33333333333333, -7.72222222222222, -8, -2.72222222222222,
> -9.27777777777778, -5.83333333333333, -4.16666666666667, -2.44444444444444,
> -1.5, -2.44444444444444, -2.44444444444444, -6.77777777777778,
> -8.05555555555556, -2.61111111111111, -1.66666666666667, -16.1666666666667,
> -17.7777777777778, -10.7777777777778, -9.83333333333333, -5.05555555555556,
> -13.8333333333333, -8.88888888888889, 2.88888888888889, -2, -12.7777777777778,
> -14.6111111111111, -10.5, -7.61111111111111, -6.72222222222222,
> -3.83333333333333, 0.111111111111113, -9.72222222222222, -16.5,
> -16.6666666666667, -6.72222222222222, -7.61111111111111, -2,
> 0.944444444444446, -4.72222222222222, -6.72222222222222, -0.222222222222221,
> -2.16666666666667, -6, -11.0555555555556, -7.66666666666667,
> -2.66666666666667, -10, -6.11111111111111, -2.22222222222222,
> -1.77777777777778, -7.44444444444444, -4.66666666666667, 0.0555555555555563,
> 0.944444444444446, -7.72222222222222, -7.44444444444444, -8.72222222222222,
> -9.22222222222222, -5.27777777777778, 1.72222222222222, 0.0555555555555563,
> -1.05555555555555, -6.66666666666667, -4, -0.444444444444445,
> 4.38888888888889, -2.33333333333333, 0.555555555555556, 1.38888888888889,
> 4.22222222222222, 4.38888888888889, 4.16666666666667, 6.11111111111111,
> 7.83333333333333, 5, 1.16666666666667, 1.55555555555555, 2.16666666666667,
> 4.38888888888889, 5.05555555555556, 8.77777777777778, 13.6666666666667,
> 16.0555555555556, 14.5, 13.9444444444444, 16.1111111111111, 17.6111111111111,
> 16.8333333333333, 13.3333333333333, 14.6666666666667, 15.6666666666667,
> 14.3333333333333, 11.1666666666667, 4.77777777777778, 3.66666666666667,
> 5.44444444444444, 8.38888888888889, 11.2777777777778, 10.6666666666667,
> 11, 12.3888888888889, 11.4444444444444, 12.5555555555556, 9.88888888888889,
> 13.1666666666667, 13.8888888888889, 14.1111111111111, 15.5, 17,
> 15.3333333333333, 12, 12.4444444444444, 10.8888888888889, 8,
> 10.2222222222222, 8.33333333333333, 8.5, 12, 14.2222222222222,
> 15.5, 15.6666666666667, 13, 9.16666666666667, 14.4444444444444,
> 15.8333333333333, 15.1111111111111, 14.4444444444444, 15.4444444444444,
> 14.3333333333333, 15.6111111111111, 17, 19.5, 20.7777777777778,
> 25.1111111111111, 25.4444444444444, 24.5, 18.5, 16.6111111111111,
> 20.1111111111111, 22.5, 20.3333333333333, 17.5555555555556, 15.5,
> 14, 13.8888888888889, 16, 18.7777777777778, 19.6111111111111,
> 15.6666666666667, 17.5555555555556, 20.1111111111111, 21.0555555555556,
> 18.8333333333333, 18.3888888888889, 21.5555555555556, 18.6666666666667,
> 19.0555555555556, 20.6666666666667, 19.8888888888889, 18.5, 20.3333333333333,
> 20.8333333333333, 23.2777777777778, 24.4444444444444, 24.4444444444444,
> 18.9444444444444, 17.5555555555556, 20.2222222222222, 22.4444444444444,
> 19.8333333333333, 19.0555555555556, 20.5, 22.3888888888889, 21.7777777777778,
> 22.5, 21.1111111111111, 20.9444444444444, 19.8333333333333, 19.3333333333333,
> 19.7222222222222, 20.3333333333333, 20.8333333333333, 20.6666666666667,
> 19.6666666666667, 19.6111111111111, 19.2222222222222, 20.1666666666667,
> 20.7777777777778, 20.5, 19.1111111111111, 20.7777777777778, 20.1111111111111,
> 20.2222222222222, 17.5, 17.4444444444444, 17.7222222222222, 17.5,
> 18.2222222222222, 18.2222222222222, 16.1111111111111, 18.2777777777778,
> 18.5, 17.9444444444444, 20.1111111111111, NA, 15.1111111111111,
> 15.3888888888889, 18.5555555555556, 20.7777777777778, 22.7222222222222,
> 21.8333333333333, 17.6666666666667, 15, 17.6666666666667, 19.8333333333333,
> 17.9444444444444, 20.3333333333333, 19.3888888888889, 20.3888888888889,
> 22.6111111111111, 23.0555555555556, 21.9444444444444, 21.5, 18.5555555555556,
> 14.2777777777778, 13.7222222222222, 15.0555555555556, 12.2777777777778,
> 13.6666666666667, 14.8888888888889, 13.2777777777778, 20.8333333333333,
> 16.5555555555556, 9.11111111111111, 14.2222222222222, 10.6666666666667,
> 9.61111111111111, 14.6666666666667, 12.6111111111111, 9.61111111111111,
> 8.94444444444444, NA, 17.2777777777778, 16.4444444444444, 15.3333333333333,
> 17, 12.1111111111111, 12.1111111111111, 12, 8.44444444444445,
> 6.27777777777778, 7.05555555555556, 6.94444444444444, 7.61111111111111,
> 6.83333333333333, 8.94444444444444, 15, 11.8333333333333, 11.7222222222222,
> 13.1666666666667, 16.4444444444444, 14.5555555555556, 10.1111111111111,
> 11.6111111111111, 5.83333333333333, 4.94444444444444, 5.44444444444444,
> 6.88888888888889, 6.83333333333333, 3.44444444444445, 2.55555555555556,
> 4.27777777777778, 8.27777777777778, 8.94444444444444, 7.55555555555556,
> 4.16666666666667, -0.111111111111111, -0.0555555555555563, 6.33333333333333,
> 3.66666666666667, -0.777777777777777, -0.388888888888889, 8.61111111111111,
> 12.3888888888889, 8.94444444444444, 10.2222222222222, 8, 6.5,
> 3.72222222222222, 1.50000000000000, 0, -0.222222222222221, 9.77777777777778,
> 10.0555555555556, 3.94444444444445, -1.27777777777778, -3.33333333333333,
> -4.72222222222222, -4.38888888888889, -7.44444444444444, -8.77777777777778,
> -7.88888888888889, -3.61111111111111, 0.111111111111113, 0.333333333333334,
> -1.22222222222222, 0.555555555555556, -1.55555555555556, -0.833333333333333,
> 1.22222222222222, 0.833333333333333, 0.277777777777778, 1.44444444444445,
> -6.27777777777778, -5.72222222222222, -5.5, -18.3333333333333,
> -12.2777777777778, -6.61111111111111, -14.5, -9.5, -17.4444444444444,
> -8.27777777777778, 4.33333333333333, -5.55555555555556, -7.33333333333333,
> -4.55555555555556, -12.8888888888889, -16, -13.8888888888889,
> -14.3888888888889, -15.5555555555556, -2.66666666666667, -0.388888888888889,
> -11, -4.22222222222222, 6.38888888888889, 0.444444444444443,
> -4.88888888888889, -14.5555555555556), duration = 1:366), row.names = 4749:5114, .Names = c("month",
> "days", "year", "temps", "duration"), class = "data.frame") 
> #================================================================
> 
> R version 2.10.0 (2009-10-26) 
> i386-pc-mingw32 
> 
> locale:
> [1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252
> [4] LC_NUMERIC=C                    LC_TIME=English_Canada.1252    
> 
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods   base     
> 
> other attached packages:
> [1] ggplot2_0.8.6 digest_0.4.2  reshape_0.8.3 plyr_0.1.9    proto_0.3-8  
> 
> 
> 
>       __________________________________________________________________
> The new Internet Explorer® 8 - Faster, safer, easier.  Optimized for Yahoo!  Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list