如何手动将线型分配给绘图中的线

How to manually assign linetype to lines in plotly

我有这样的情节:

library(plotly)
library(tidyr)
library(dplyr)

data.frame(date = seq(ymd('2020-01-01'), ymd('2020-05-31'), by = '1 day'), 
           sin = sin(seq(-pi, pi, length.out = 152)), 
           cos = cos(seq(-pi, pi, length.out = 152))) %>% 
  gather(k, v, -date) %>% 
  plot_ly() %>% 
  add_lines(x = ~date, y = ~v, linetype = ~k, color = I('black'))

输出如下所示:

我想做的是手动将线型分配给k变量的值,例如,将虚线分配给cos,将实线分配给sin no他们的顺序问题。基本上我正在寻找 plotlyggplot2::scale_linetype_manual 的等价物?如果不为每一行添加新的跟踪,我怎样才能实现这一目标? (我事先不知道 k 值的确切数量)

类似于,这可以通过使用线型的命名向量来实现,如下所示:

library(plotly)
library(tidyr)
library(dplyr)
library(lubridate)

lty <- c(cos = "dash", sin = "solid")

data.frame(date = seq(ymd('2020-01-01'), ymd('2020-05-31'), by = '1 day'), 
           sin = sin(seq(-pi, pi, length.out = 152)), 
           cos = cos(seq(-pi, pi, length.out = 152))) %>% 
  gather(k, v, -date) %>% 
  plot_ly() %>% 
  add_lines(x = ~date, y = ~v, linetype = ~k, color = I('black'), linetypes = lty)