R 如何向时间序列图添加文本注释

R 如何向时间序列图添加文本注释,r,ggplot2,posixct,annotate,R,Ggplot2,Posixct,Annotate,我知道annotate(),需要添加x和y坐标。问题是我的x是类“POSIXct”POSIXt。所以当我尝试使用annotate时,R响应,对象需要是POSIXct。我尝试了各种组合试图解决这个问题。。。没有成功。有什么想法吗 确保annotate中的x参数编码为POSIXct。例如,使用lattice软件包中的大麦数据集,我们可以将年份重新编码为POSIXct,然后注释: library(lattice) library(tidyverse) barley %>% #convert

我知道annotate(),需要添加x和y坐标。问题是我的x是类“POSIXct”POSIXt。所以当我尝试使用annotate时,R响应,对象需要是POSIXct。我尝试了各种组合试图解决这个问题。。。没有成功。有什么想法吗

确保annotate中的x参数编码为POSIXct。例如,使用lattice软件包中的大麦数据集,我们可以将年份重新编码为POSIXct,然后注释:

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")

代码在哪里,这样其他人就不必为您模拟示例了?还有,为什么不把
lt
转换成'ct'?