How can I persuade ggplot2 geom_text to label a specified date in a time series plot?

23,572

To add text to ggplot, use geom_text:

Method 1: Add a column of labels to your data.frame:

tmp$note <- LETTERS[1:7]

ggplot(tmp,aes(date, price, label=note)) +
  geom_line() +
  geom_text(vjust=0, colour="red")

enter image description here

Method 2: Add a specific label:

ggplot(tmp,aes(date, price, label=date)) +
  geom_line() +
  geom_text(data=tmp[3, ], label="Something of note", vjust=1)

enter image description here

Share:
23,572
SlowLearner
Author by

SlowLearner

Write inelegant code with sporadic enthusiasm, mostly in R, but occasionally Perl, VBA etc.

Updated on August 23, 2020

Comments

  • SlowLearner
    SlowLearner over 3 years

    I am using ggplot2 to plot simple line charts of time series data. One difficulty I have run into is labelling specific points corresponding to x-axis values i.e. dates.

    library(ggplot2)
    library(scales)
    date <- c("2011-09-19","2011-09-20","2011-09-21",
        "2011-09-22","2011-09-23","2011-09-26","2011-09-27")
    price <- c(100,110,105,115,120,115,125)
    tmp <- data.frame(date,price)
    tmp$date <- as.Date(tmp$date)
    p <- ggplot(tmp,aes(tmp$date,tmp$price))
    p <- p + xlab("Date")
    p <- p + ylab("Price")
    p <- p + layer(geom = "line")
    p <- p + opts(title="Simple price plot")
    print(p)
    

    What I would like to do is add an annotation to a specific date, which might be a maximum or a minimum value or something else of note. So far all the permutations of geom_text I have used have failed to get the effect I want (or indeed anything useful). There are a few questions on this on SO but most seem related to scatter charts rather than time series; I haven't been successful in trying to adapt them. I have also spent some time with the documentation but my understanding is still limited. Any pointers would be appreciated.

  • SlowLearner
    SlowLearner over 12 years
    Andrie, thanks for this, I understand it a little better now. So I can do something like: p <- ggplot(tmp,aes(date, price, label=date)) + geom_line() + geom_text(data=tmp[which.max(tmp[,2]), ], label="Max",vjust=-1) and follow it up with p <- p + geom_text(data=tmp[which.min(tmp[,2]), ], label="Min",vjust=1. I am still working my way round to inserting the actual max/min value. I get confused by the different structures, how to access them and which packages do or don't accept them!
  • Andrie
    Andrie over 12 years
    In the case of ggplot it's straight-forward: the data structure must always be a data.frame. You also don't have to create a separate layer for each label. Your easiest option is to create a single data.frame with the location and text for each label, and pass that in a single layer to ggplot.
  • SlowLearner
    SlowLearner over 12 years
    I tried labelling a density plot using ggplot(tmp,aes(x=price,label=date)) + geom_density() + geom_text(data=tmp[3, ], label="Something of note", vjust=1) but this gave "Error: geom_text requires the following missing aesthetics: y" which seems as if it might be related to the stat in some way?
  • Andrie
    Andrie over 12 years
    It sounds like your data doesn't have a y-position. You'll either have to add a y-position manually for the label, or use stat-density and map the y-value to a supplied statistic.