Doug Mitarotonda
unread,Oct 29, 2013, 6:09:08 PM10/29/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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