繁体   English   中英

如何使用时间序列 plot 在 r plotly 中添加虚线垂直线

[英]How to add a dash vertical line in r plotly with time series plot

我有一个看起来像这样的时间序列:

+------------+-------+------+
| Dates      | Sales | City |
+------------+-------+------+
| 2020-01-01 | 10    | A    |
+------------+-------+------+
| 2020-01-02 | 20    | A    |
+------------+-------+------+
| 2020-01-03 | 30    | A    |
+------------+-------+------+
| 2020-01-04 | 40    | A    |
+------------+-------+------+
| 2020-01-05 | 50    | A    |
+------------+-------+------+
| 2020-01-01 | 60    | B    |
+------------+-------+------+
| 2020-01-02 | 70    | B    |
+------------+-------+------+
| 2020-01-03 | 50    | B    |
+------------+-------+------+
| 2020-01-04 | 30    | B    |
+------------+-------+------+
| 2020-01-05 | 60    | B    |
+------------+-------+------+

我想将 plot 这两个时间序列一起使用 plotly,针对不同的城市使用不同的 colors。 然后在日期 2020-01-03 添加一条垂直虚线(ofc 这只是一个简单的伪数据集)

它看起来像这样: 在此处输入图像描述

我停在这里:

plotly(x = ~df$Dates) %>%
 add_lines( y = ~df$Sales, color = ~df$City)

如何添加锚定到特定日期的垂直虚线

PS这不是一个重复的问题 - 使用基本 R 或 ggplot 在 SO 上有一些类似的问题,但没有一个足够接近使用 ZE1E1D3D40573127E9EE0480DZ12 解决 Plotly 中的这个特定问题。

非常感谢您的帮助!

使用add_segments一种方法:

library(plotly)

plot_ly(df, x = ~Dates, y = ~Sales, type = 'scatter', mode = 'lines', color = ~City) %>%
  add_segments(y = 0, yend = 70, x = as.Date('2020-01-03'), xend = as.Date('2020-01-03'), 
               line = list(dash = "dash"), showlegend=FALSE)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM