geom_text and time data

266 views
Skip to first unread message

John Kane

unread,
Dec 20, 2013, 12:46:38 PM12/20/13
to ggp...@googlegroups.com
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

Ben Bond-Lamberty

unread,
Dec 20, 2013, 1:04:39 PM12/20/13
to John Kane, ggplot2
Hi John, how about simply doing

p1+annotate('text',ymd("2008-01-01"),265,label="Large Cities")

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Ista Zahn

unread,
Dec 20, 2013, 1:53:44 PM12/20/13
to John Kane, ggplot2
The arguments for geom_text are

args(geom_text)
function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
parse = FALSE, ...)

like most geom_* functions, the first argument is an aesthetic
mapping, usually created with aes. If you want to override the x and y
position mappings you can, but you are certainly going to have to name
them. Otherwise geom_text is going to assume that you've supplied the
arguments in order, i.e., that as.numeric(cityname$cit[1]) is the
mapping. Since this is an invalid mapping, it doesn't work. All this
to say that you just need to name your arguments:

p1 + geom_text(x = as.numeric(cityname$cit[1]), y = 265, label =
"Large Cities")

Best,
Ista


On Fri, Dec 20, 2013 at 12:46 PM, John Kane <jrkr...@gmail.com> wrote:

John Kane

unread,
Dec 20, 2013, 4:01:23 PM12/20/13
to ggp...@googlegroups.com


On Friday, December 20, 2013 12:46:38 PM UTC-5, John Kane wrote:
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.

Thanks gentlemen, both methods are working. 

Ben,
The reason for not using annotate is that I had forgotten it existed.

Ista,
Thanks for the x explaination. I had thought I was okay by the order of the commands.
Reply all
Reply to author
Forward
0 new messages