define color scheme when plotting line plot with plotly

爱⌒轻易说出口 提交于 2021-01-28 07:41:27

问题


I have the following data table (which is not always the same, and has always a different number of columns) and code for plotting a line chart:

dt <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                 Germany = rnorm(365, 2, 1), Austria = rnorm(365, 3, 4), 
                 Czechia = rnorm(365, 2, 3), check.names = FALSE)

colNames <- names(dt)[-1]           ## assuming date is the first column
p <- plotly::plot_ly()

for(trace in colNames){
  p <- p %>% plotly::add_trace(data = dt, x = ~date, y = as.formula(paste0("~`", trace, "`")), name = trace,
                               type = 'scatter', mode = 'lines', connectgaps = TRUE, 
                               hovertemplate = paste("%{xaxis.title.text}:  %{x}<br>",
                                                     "%{yaxis.title.text}:  %{y}<br>") )
}

p %>% 
  layout(title = "Coal",
         xaxis = list(title = "Date"),
         yaxis = list (title = "\u20ac/MWh")) 

This yields the following plot:

I would like to create a different color selection in different shades of green (darkest = "#007d3c", "#419F44" and lightest = "#81C07A").

How can I do this???


回答1:


Perhaps try

colNames <- names(dt)[-1] ## assuming date is the first column
colors <- setNames(c("#007d3c", "#419F44", "#81C07A"), colNames)
line_type <- setNames(c("solid", "dot", "dash"), colNames)
p <- plotly::plot_ly()

for(trace in colNames){
  p <- 
    p %>% 
    plotly::add_trace(
      data = dt, x = ~date, y = as.formula(paste0("~`", trace, "`")), name = trace,
      type = 'scatter', mode = 'lines', connectgaps = TRUE, 
      hovertemplate = paste("%{xaxis.title.text}:  %{x}<br>",
                            "%{yaxis.title.text}:  %{y}<br>"), 
      line = list(color = colors[[trace]], dash = line_type[[trace]])
    )
}


p %>% 
  layout(
    title = "Coal",
    xaxis = list(title = "Date"),
    yaxis = list (title = "\u20ac/MWh")
  ) 

Output chart looks like this

Corrected the line_type vector as per @ismirsehregal's post




回答2:


There is no linetype line I guess you want solid:

Valid linetypes include: 'solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot'

The following avoids repetitive calls of add_trace (for loop) using plot_ly's color/colors and linetype/linetypes arguments:

library(plotly)
library(data.table)

DT <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                 Germany = rnorm(365, 2, 1), Austria = rnorm(365, 3, 4), 
                 Czechia = rnorm(365, 2, 3), check.names = FALSE)

colNames <- setdiff(copy(names(DT)), "date")
colors <- setNames(c("#007d3c", "#419F44", "#81C07A"), colNames)
linetypes <- setNames(c("solid", "dot", "dash"), colNames)

DT <- melt.data.table(DT, id.vars = "date")

p <- plot_ly(DT, x = ~ date, y = ~ value, color = ~ variable, colors = colors, type = 'scatter', mode = 'lines', linetype = ~ variable, linetypes = linetypes,
             connectgaps = TRUE, name = DT[, 2],
             hovertemplate = paste("%{xaxis.title.text}:  %{x}<br>",
                                   "%{y} \u20ac/MWh <br>")) %>%
  layout(title = "<b>Coal", xaxis = list(title = "Date"), 
         yaxis = list(title = "EUR/MWh"), showlegend = FALSE)

p

Please also see this.



来源:https://stackoverflow.com/questions/64440028/define-color-scheme-when-plotting-line-plot-with-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!