r语言 - 如何向ts_plot添加垂直线



>我从Twitter用户那里下载了一个时间线,并尝试可视化推文数量随时间的变化。我正在用 rtweets ts_plot来做。现在我正在尝试在我的图表中添加一条垂直线。据我所知,ts_plot允许您像使用普通的ggplot一样使用它。因此,我尝试了ggplot2的geom_vline:

这是我的代码:

zanetti <- get_timeline("@zac1967", n=3200)
ts_plot(zanetti, "days") +
  theme_bw() +
  xlab("") +
  ylab("# of tweets/day") +
  geom_vline( aes(xintercept = "2019-03-21 00:00:00 UTC"))

但是,我收到此错误消息:

  no applicable method for 'rescale' applied to an object of class "character" 

所以我尝试了相同的代码,但在最后一行添加了 as.numer:

ts_plot(zanetti, "days") +
  theme_bw() +
  xlab("") +
  ylab("# of tweets/day") +
  geom_vline( aes(xintercept = as.numeric("2019-03-21 00:00:00 UTC")))

这会导致以下错误消息:

Warning messages:
1: In FUN(X[[i]], ...) : NAs introduced by coercion
2: Removed 53 rows containing missing values (geom_vline). 

首先,您不需要使用 aes(),因为您没有映射到列名。

ts_plot的 x 轴刻度是日期时间刻度,因此您需要相应地转换该值。这样的事情应该有效:

+ geom_vline(xintercept = as.POSIXct("2019-03-21 00:00:00", tz = "UTC"))

最新更新