根据ggplot中其他列中的值在ggplot中包含垂直线

Include vertical line in ggplot based on value in other column in ggplot

这是一个令人困惑的标题,但我有一个 df(大得多,但是)像这样:

df
# A tibble: 10 × 3
    week count protest
   <int> <dbl>   <dbl>
 1     1  259.       0
 2     2  509.       0
 3     3  556.       0
 4     4  588.       0
 5     5  541.       0
 6     6  576.       1
 7     7  531.       0
 8     8  518.       0
 9     9  470.       1
10    10  392.       1

我想要的是在 x 轴上绘制 countweek 的线。但我还想为每个包含 protest == 1.

值的 week 添加一条垂直线

我做过这样的事情:

library(ggplot2)
ggplot(berlin_week, aes(x=week, y=count)) +
   geom_line() +
  geom_vline(aes(xintercept= week[match(1, protest)]), col = "red")

但是如您所见,我只得到第一个具有 protest == 1 的观察结果,而不是我想要的所有具有 protest == 1 的观察结果。有任何想法吗? 谢谢!

要为抗议等于 1 的任何一周添加一条红线,您可以使用以下代码

ggplot(berlin_week, aes(x=week, y=count)) +
  geom_line() +
  geom_vline(xintercept = berlin_week$week[berlin_week$protest == 1], color = "red")