为什么 plotly 条形图在 R 中不按顺序使用我指定的颜色,我如何强制它按顺序使用我的颜色?
Why does plotly bar chart not use my specified colors in order in R, and how do I force it to use my colors in order?
我有一个数据集,我想将其转换为 R Shiny 中的图表。我还有一长串颜色,但它们需要用于任何类别,但必须按顺序使用(例如,绘图不应使用第 4 种颜色,除非使用前 3 种颜色,依此类推)。
出于某种原因,仅针对我的条形图(我的饼图没有这个问题),随机选择了乱序的颜色。例如,这里是数据:
library(dplyr)
test <- tibble(project = c("big", "medium", "big", "medium"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
这是我拥有所有颜色时的情节:
all_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = all_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
但是当我只包含前 2 种颜色时,它看起来是正确的:
some_colors <- c("#CA001B", "#1D28B0")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = some_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
如何强制 plotly 按顺序使用我的颜色? 我不想将颜色映射到特定变量,因为未来的可视化可能有更多类别。出于同样的原因,我不想只包含最少数量的可行颜色。
不确定内部结构。看起来像是从最后一种开始巧妙地挑选颜色。一种解决方法是使用 colors = ~all_colors[seq_along(unique(project))])
:
library(dplyr)
library(plotly)
test <- tibble(project = c("big", "medium", "big", "medium"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
all_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")
plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project,
color = ~project, colors = ~all_colors[seq_along(unique(project))]) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), barmode = "stack")
test <- tibble(project = c("big", "low", "medium", "superbig"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project,
color = ~project, colors = ~all_colors[seq_along(unique(project))]) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), barmode = "stack")
我有一个数据集,我想将其转换为 R Shiny 中的图表。我还有一长串颜色,但它们需要用于任何类别,但必须按顺序使用(例如,绘图不应使用第 4 种颜色,除非使用前 3 种颜色,依此类推)。
出于某种原因,仅针对我的条形图(我的饼图没有这个问题),随机选择了乱序的颜色。例如,这里是数据:
library(dplyr)
test <- tibble(project = c("big", "medium", "big", "medium"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
这是我拥有所有颜色时的情节:
all_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = all_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
但是当我只包含前 2 种颜色时,它看起来是正确的:
some_colors <- c("#CA001B", "#1D28B0")
library(plotly)
fig <- plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project, color = ~project, colors = some_colors) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), font = line_chart_text_format, barmode = "stack")
fig
如何强制 plotly 按顺序使用我的颜色? 我不想将颜色映射到特定变量,因为未来的可视化可能有更多类别。出于同样的原因,我不想只包含最少数量的可行颜色。
不确定内部结构。看起来像是从最后一种开始巧妙地挑选颜色。一种解决方法是使用 colors = ~all_colors[seq_along(unique(project))])
:
library(dplyr)
library(plotly)
test <- tibble(project = c("big", "medium", "big", "medium"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
all_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")
plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project,
color = ~project, colors = ~all_colors[seq_along(unique(project))]) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), barmode = "stack")
test <- tibble(project = c("big", "low", "medium", "superbig"),
capacity = c(10, 5, 16, 3),
date = c("Aug 2021", "Aug 2021", "Sep 2021", "Sep 2021"),
date_num = as.Date("2021-08-01", "2021-08-01", "2021-09-01", "2021-09-01")) %>%
dplyr::mutate(date = reorder(date, date_num))
plot_ly(test, x = ~date, y = ~capacity, type = 'bar', name = ~project,
color = ~project, colors = ~all_colors[seq_along(unique(project))]) %>%
layout(legend = list(orientation = 'h', x = .5, xanchor = "center", y = -.3), barmode = "stack")