-2

I know about annotate() and that you need to add the x and y coordinates. The problem is my x is class ""POSIXct" "POSIXt." So when I try to use annotate, R responds, object needs to be POSIXct. I have tried various combinations trying to fix this ... no success. Any ideas?

user8369515
  • 485
  • 1
  • 5
  • 16
  • Where's the code so others don't have to mock up an example for you? Also, why not just convert the `lt` to ` ct`? – hrbrmstr Oct 16 '18 at 19:30

1 Answers1

3

Make sure the x argument in annotate is coded as POSIXct. For example, using the barley dataset from the lattice package, we can recode the year to POSIXct and then annotate:

library(lattice)
library(tidyverse)

barley %>%
  #convert year from factor to numeric, and then to POSIXct
  mutate(year = as.numeric(levels(year))[year],
         year = as.POSIXct(paste0(year, "-01-01"))) %>% 
  group_by(year) %>% 
  summarise(AvgYield = mean(yield)) %>% 
  ggplot(aes(year, AvgYield)) + 
    geom_line() + 
    #now to annotate, just make sure to code x as POSIXct 
    #in a range that will appear on the plot
    annotate("text", x = as.POSIXct("1931-04-01"), y = 34, label = "Some text")
Jordo82
  • 796
  • 4
  • 14