2

I have a ggplot2 graph with datetime on the x axis, and categorical on the y axis. I need to put 9 text annotations at certain dates on the x-axis, but there is no space to put the annotations within the graph itself. I want to keep the auto generated dates on the x-axis, and add my custom labels too. I don't care if they're at the top or the bottom.

Synthetic example below. I have 9 events, and lots of activity leading up to these events. I want to put a text label indicating the event name below each event. Eg in this data at Feb 11th at midnight I want to have a label that says "Event 1" somewhere.

events <- data.frame(names=c("Event 1", "Event 2", "Event 3", "Event 4", "Event 5", "Event 6", "Event 7", "Event 8", "Event 9"),
                       dates=strptime(c("2013-02-10 11:59 pm", "2013-02-21 11:59 pm", "2013-03-02 11:59 pm", "2013-03-16 11:59 pm", "2013-03-26 12:00 pm", "2013-04-11 11:59 pm", "2013-04-24 11:59 pm", "2013-04-23 11:59 pm", "2013-05-08 12:00 pm")
                                      , "%Y-%m-%d %I:%M %p"))
units <- replicate(125, paste(sample(LETTERS, 8), collapse=""))
n <- 2000
df <- data.frame(name=sample(units, n, replace=TRUE),
           d=(sample(events$dates, n, replace=TRUE)-rexp(n, 1/150000)),
            value=rexp(n, 1/110))

ggplot(data=df, aes(x=d, y=name)) + 
    geom_point(alpha=I(1/2), aes(size=log(value))) +
    ylab("Each row is one unit") +
    xlab("Date") +
    theme(legend.position="none", legend.direction="horizontal",
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.line.y=element_blank(),
          panel.grid.major.y=element_blank(),
          panel.grid.minor.y=element_blank())

Generated synthetic image

chmullig
  • 13,006
  • 5
  • 35
  • 52
  • 3
    [Here](http://stackoverflow.com/questions/16279295/axis-labels-for-each-bar-and-each-group-in-bar-charts-with-dodged-groups/16279990#16279990) is example on putting text under the plot in specific positions. – Didzis Elferts May 09 '13 at 17:07
  • @DidzisElferts thanks! I fixed the dataframe issue, I had missed a rename at the time, and apparently my R session still had it so I didn't notice. The code should work fine now. – chmullig May 09 '13 at 17:48

1 Answers1

5

You can use geom_text() to plot labels inside plot. As x values use dates in data frame events and as y value set some negative number to plot labels under the points. With vjust= and hjust= you can adjust placement of labels (center of date or left/right).

ggplot(data=df, aes(x=d, y=name)) + 
  geom_point(alpha=I(1/2), aes(size=log(value))) +
  geom_text(data=events,aes(x=dates,y=-5,label=names),inherit_aes=FALSE,
            vjust=-1,hjust=0,size=3)+
  ylab("Each row is one unit") +
  xlab("Date") +
  theme(legend.position="none", legend.direction="horizontal",
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.line.y=element_blank(),
        panel.grid.major.y=element_blank(),
        panel.grid.minor.y=element_blank())

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • 1
    Useful, but I'd really prefer it below (or above) the axis, since my real data is messier than the simulation. Labeling inside pretty much means I have to scoot it up, as you did here, which looks a little odd. – chmullig May 09 '13 at 18:40