[R] Placing text in a ggplot

Felipe Carrillo mazatlanmexico at yahoo.com
Sat Oct 10 20:33:28 CEST 2009


John:
The 'year' dataset has 366 rows, I used the 'line.count' without the '1' to come up with 12 rows to match the mlabs and then used line.count to draw the labels. Is this close to what you want?

line.count <- c(cumsum(as.vector((table(year$monthnum)))));line.count
namposts <- line.count;namposts
temprange <- range(year[,4], na.rm=TRUE);temprange <- max(temprange);temprange
mlabs <- month.name[1:12];mlabs

p  <- ggplot(year, aes(duration, temps, colour=month)) + geom_line() +
       ylab("Temperature (C)") + xlab("Mean Daily Temperatures")  +
       opts(legend.position = "none", title="Ottawa, 2008",
       axis.text.x = theme_blank(), axis.ticks = theme_blank()) +
       geom_vline(xintercept= line.count)

p

p +  geom_text(aes(x = namposts, y = temprange, label = mlabs),
     data = as.data.frame(line.count), colour='black', size=2.5,hjust = 1, vjust = 0)



Felipe D. Carrillo  
Supervisory Fishery Biologist  
Department of the Interior  
US Fish & Wildlife Service  
California, USA


--- On Fri, 10/9/09, John Kane <jrkrideau at yahoo.ca> wrote:

> From: John Kane <jrkrideau at yahoo.ca>
> Subject: [R] Placing text in a ggplot
> To: "R R-help" <r-help at stat.math.ethz.ch>
> Date: Friday, October 9, 2009, 12:59 PM
> I am attempting to graph 12 months of
> temperatures, delineate the months with a vline and place
> the names of the months at the top of the graph.
> 
> So far I have gotten everything to work except the names,
> despite getting a similar graph to work yesterday the day
> before yesterday with Baptise A's help.  Can anyone
> suggest what I am doing wrong.  Data set is below
> code.
> 
> Thanks.
> 
> Code 
> =========================================================================
> line.count <-
> c(1,cumsum(as.vector((table(year$monthnum)))))
> namposts <- line.count[1:12]
> temprange <- range(year[,4], na.rm=TRUE)
> mlabs <- month.name[1:12]
> 
> p  <- ggplot(year, aes(duration, temps,
> colour=month)) + geom_line() +
>        ylab("Temperature (C)") +
> xlab("Mean Daily Temperatures")  +
>        opts(legend.position =
> "none", title="Ottawa, 2008",
>        axis.text.x =
> theme_blank(), axis.ticks = theme_blank()) +
>        geom_vline(xintercept=
> line.count) 
> 
> [[elided Yahoo spam]]
>        
> p +  geom_text(aes(x = namposts, y = temprange[2],
> label = mlabs),
>      data = year, size = 2.5,
> colour='black', hjust = 0, vjust = 0)
> 
> ======================================================================
> 
> year  <- structure(list(monthnum = 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, 3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 7L, 7L, 7L, 7L, 7L, 7L, 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, 6L, 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, 2L, 2L, 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, 
> 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, 11L, 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, 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), .Label =
> c("April", 
> "August", "December", "February", "January", "July",
> "June", 
> "March", "May", "November", "October", "September"), class
> = "factor"), 
>     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("monthnum", 
> "days", "month", "temps", "duration"), class =
> "data.frame")
> 
> ______________________________________________
> 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