Annotate a plot in the upper corner with datetime on the x-axis

2,987 views
Skip to first unread message

Doug Mitarotonda

unread,
Oct 29, 2013, 6:09:08 PM10/29/13
to ggplot2
Hi,
I am trying to annotate a plot so text appears in the very upper left corner of a plot. This is pretty easy using Inf when the scale is numeric or character, but when the scale is a date, I am having trouble. For example,

library(ggplot2)
library(lubridate)
# Numerics work fine
df_num <- data.frame(A = 1:2, B = 1:2)
ggplot(df_num, aes(x = A, y = B)) + geom_point() +
annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = 1, label = "Hello world!")
# Characters work fine
df_chr <- data.frame(A = c("X", "Y"), B = 1:2)
ggplot(df_chr, aes(x = A, y = B)) + geom_point() +
annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = 1, label = "Hello world!")
# Dates are more challenging. A similar approach causes an error.
df_dte <- data.frame(A = mdy(c("01/01/2013", "02/01/2013")), B = 1:2)
#ggplot(df_dte, aes(x = A, y = B)) + geom_point() +
# annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = 1, label = "Hello world!")
#Error: Invalid input: time_trans works with objects of class POSIXct only
# The best I have come up with is using the min date, but it is not flush to the left
ggplot(df, aes(x = A, y = B)) + geom_point() +
annotate("text", x = min(df$A), y = Inf, hjust = 0, vjust = 1, label = "Hello world!")

Any help is greatly appreciated!

Best wishes,
Doug

baptiste auguie

unread,
Oct 29, 2013, 6:15:09 PM10/29/13
to Doug Mitarotonda, ggplot2
Hi,

You can use this trick,

g = grobTree(textGrob("Hello world!", x=0, hjust=0, y=1, vjust=1))
ggplot(df_dte, aes(x = A, y = B)) + geom_point() + annotation_custom(g)

HTH,

b.




--
--
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.

Dennis Murphy

unread,
Oct 29, 2013, 7:58:47 PM10/29/13
to baptiste auguie, Doug Mitarotonda, ggplot2
The following also works without the grid package trick (although I
liked it!!), inspired by the error message re POSIXct objects in
Doug's post:

ggplot(df_dte, aes(x = A, y = B)) + geom_point() +
annotate("text", x = as.POSIXct(-Inf, origin = '1970-01-01'), y = Inf,
hjust = 0, vjust = 1, label = "Hello world!")

The origin in the conversion to POSIXct is necessary.

Dennis

Doug Mitarotonda

unread,
Oct 29, 2013, 8:13:10 PM10/29/13
to Dennis Murphy, baptiste auguie, ggplot2
Dennis,
Thanks for your suggestion. I am actually going to end up using your solution because in my real plots I am also using the "size" parameter to change the font size. As far as I can tell, the fontsize/cex options in grid don't line up with size in ggplot, so I had to manually adjust things to make my non-date plots look the same as the date plot. Now I can go back to them all using the same value!

Best wishes,
Doug

Doug Mitarotonda

unread,
Oct 29, 2013, 6:31:18 PM10/29/13
to baptiste auguie, Doug Mitarotonda, ggplot2
That is quite a snazzy trick, thanks very much!

______________________________________________________________________________________

Doug Mitarotonda, Ph.D. | Qcue | Director, Pricing & Analytics | 607.227.9364 | dmitar...@qcue.com



On Tue, Oct 29, 2013 at 3:15 PM, baptiste auguie <baptist...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages