[R] Putting names on a ggplot

John Kane jrkrideau at yahoo.ca
Sat Oct 17 23:04:43 CEST 2009


Putting names on a ggplot

Can anyone suggest what I am doing wrong here.  I am plotting
daily temperatures at Ottawa Ontario for 2008 broken down by
months, I seperate them by lines and want to put the names of the months
at the top of the chart ( with in the graphing area)

Everything is working as I want until I try to add the names of the months.
I did something similar a few days ago and I cannot see were I am doing
anything very different.

Also I tried to use year$month -- an interger variable -- to get the colour
breakdown with no success and finally added the months variable ( character)
to get it.  Is this normal for ggplot?

Thanks.

CODE-- Data is below code
==============================================================================
 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

# this is where I see the problem.

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)

p
=============================================================================

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,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 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")




More information about the R-help mailing list