0

Is it possible to use R to add the date onto a ggplot graph (upon which the graph data is based)? I have code which selects the most daily data over a the course of a week, and this subsequently gets plotted. An example dataframe (from a previous answer) is shown below:

set.seed(1234)
df <- data.frame(Date=seq(as.POSIXct("05:00", format="%H:%M"), 
                          as.POSIXct("23:00", format="%H:%M"), by="hours"))
df$Counts <- sample(19)
df <- df[-c(4,7,17,18),]

# generate the groups automatically and plot
idx <- c(1, diff(df$Date))
i2 <- c(1,which(idx != 1), nrow(df)+1)
df$grp <- rep(1:length(diff(i2)), diff(i2))
g <- ggplot(df, aes(x=Date, y=Counts)) + geom_line(aes(group = grp)) + 
  geom_point()
g

I know I can use the following code to get R to work out which day of the week my data is:

d <- as.Date("19/02/2013","%d/%m/%Y") 
format(d, "%A, %b %d, %Y")

But I obviously have to manually input the date for this to work.

Is it possible to get R to read the dataframe and work out the day of the week the data is based on (it will be only one day), and then add this as a legend in the top right hand corner of the graph? Ideally the format would include both the day and date (e.g. Tuesday, Feb 19th 2013).

Edit

I want to add the date as text in the graph. Can I use geom_text for this? This isn't working at the moment, but so far I have got:

g + geom_text(aes(x=as.POSIXct("18:00:00", format="%H:%M:%S"), y=1500), label=date_format("%A, %b %d, %Y")", size=2)

Can anyone advise me what I am doing wrong?

1
  • Not sure if this is what you want, but why don't you just pass df$Date to the format as you pass d like: format(as.Date(df$Date, "%d/%m/%Y"), "%A, %b %d, %Y") Commented Feb 19, 2013 at 12:33

1 Answer 1

2

As suggested by Arun(see comment) you can you can format your input data or add a formatting to the current plot :

 library(scales)
 g+ scale_x_datetime(labels = date_format("%A, %b %d, %Y"))

EDIT

I guess you want to annotate your graph adding a date indicator

Since you will add a single date , you can use annotate, It is better than using geom_text which will overlap many dates. For example:

 label <- unique(format(as.Date(df$Date, "%d/%m/%Y"),
                                "%A, %b %d, %Y"))
 g+ annotate("text", x = mean(df$Date), y = 20, 
       label = label,size=20, col='red')

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks @agstudy. My x axes is pretty full at the moment, so I was hoping to be able to use geom_text? I have added some code which isn't currently working into my original question.
geom_text will add many dates..you want to annotate your plot with just one date?
The label feature is vry useful @agstudy. Many thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.