使用 plotly 绘制线图时定义配色方案
define color scheme when plotting line plot with plotly
我有以下数据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 <- 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"))
这会产生以下图:
我想在不同的绿色阴影中创建不同的颜色选择(最深 = "#007d3c"
、"#419F44"
和最浅 = "#81C07A"
)。
我该怎么做???
也许试试
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")
)
输出图表如下所示
根据@ismirsehregal 的 post
更正了 line_type 向量
没有线型line
我猜你想要solid
:
Valid linetypes include: 'solid', 'dot', 'dash', 'longdash',
'dashdot', 'longdashdot'
以下避免使用 plot_ly 的 color/colors
和 linetype/linetypes
参数重复调用 add_trace
(for 循环):
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
请参阅 this。
我有以下数据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 <- 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"))
这会产生以下图:
我想在不同的绿色阴影中创建不同的颜色选择(最深 = "#007d3c"
、"#419F44"
和最浅 = "#81C07A"
)。
我该怎么做???
也许试试
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")
)
输出图表如下所示
根据@ismirsehregal 的 post
更正了 line_type 向量没有线型line
我猜你想要solid
:
Valid linetypes include: 'solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot'
以下避免使用 plot_ly 的 color/colors
和 linetype/linetypes
参数重复调用 add_trace
(for 循环):
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
请参阅 this。