0

我从 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.numeric:

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). 
4

1 回答 1

0

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

x 轴刻度ts_plot是日期时间刻度,因此您需要相应地转换该值。像这样的东西应该工作:

+ geom_vline(xintercept = as.POSIXct("2019-03-21 00:00:00", tz = "UTC"))
于 2019-04-30T01:24:59.273 回答