簡體   English   中英

如何在ts_plot中添加垂直線?

[英]How can I add a vertical line to a 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.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). 

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

ts_plot的x軸刻度是日期時間刻度,因此您需要相應地轉換該值。 這樣的事情應該有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM