I am trying to put some text on a graph with geom_text which is easy enough in most cases but I am at a loss how to do it when my x-variable is time.
The annoying thing is I found a method that worked fine for identifying the point where I wanted to put an abline. I had thought that I could use the same method to identify the x-axis point for use in geom_text but either I am making some stupid typing/syntax error or I am totally wrong in my approach.
When it comes right down to it, I don't have to use time data on this simple graph it just seemed like a good exercise (he said 2 hours later).
Data and code below
Any suggestions would be appreciated.
john
dat1 <- structure(list(city = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("city", "dc"), class = "factor"),
score = c(243, 245, 248, 251, 255, 260, 262, 265, 269, 271,
274, 276), year = structure(c(1041379200, 1104537600, 1167609600,
1230768000, 1293840000, 1356998400, 1041379200, 1104537600,
1167609600, 1230768000, 1293840000, 1356998400), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), .Names = c("city", "score", "year"
), row.names = c(NA, -12L), class = "data.frame")
library(ggplot2)
library(lubridate)
rees <- data.frame(turnover = ymd("2008/01/01")) #vline point
cityname <- data.frame(cit = ymd("2008/01/01"))
p <- ggplot(dat1, aes(year, score, colour = city)) + geom_line() + geom_point()
p <- p + xlab("Year") + ylab("Average scores") + ggtitle("8th grade math scorers")
p1 <- p + theme(legend.position = "blank", plot.title = element_text(size = rel(1.5))) +
geom_vline( xintercept= as.numeric(rees$turnover[1]))
p1
p2 <- p1 + geom_text(as.numeric(cityname$cit[1]), 265, label = "Large Cities")
p2