为啥 plotly 条形图在 R 中不按顺序使用我指定的颜色,我如何强制它按顺序使用我的颜色?

Posted

技术标签:

【中文标题】为啥 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?为什么 plotly 条形图在 R 中不按顺序使用我指定的颜色,我如何强制它按顺序使用我的颜色? 【发布时间】:2021-10-13 10:02:23 【问题描述】:

我有一个数据集,我想在 R Shiny 中将其变成一个绘图图表。我也有一长串颜色,但它们需要用于任何类别,但必须按顺序使用(例如,除非前三种颜色已经使用,否则绘图不应使用第 4 种颜色,依此类推)。

出于某种原因,只是为了我的条形图(饼图没有这个问题),阴谋地选择乱序的颜色。例如,这里是数据:

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

但是当我只包含前两种颜色时,它看起来是正确的:

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 按顺序使用我的颜色?我不想将颜色映射到特定变量,因为未来的可视化可能会有更多类别。出于同样的原因,我不想只包含最小数量的可行颜色。

【问题讨论】:

【参考方案1】:

不确定内部结构。看起来像情节从最后一个开始挑选颜色。一种解决方法是使用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")

【讨论】:

以上是关于为啥 plotly 条形图在 R 中不按顺序使用我指定的颜色,我如何强制它按顺序使用我的颜色?的主要内容,如果未能解决你的问题,请参考以下文章

熊猫数据框条形图在条形之间放置空间

ggplotly堆积条形图在过滤后不调整大小

R Shiny中的Plotly分组条形图

R Plotly:右侧 Y 轴的水平条形图

R语言ggplot2可视化绘制条形图(bar plot)使用gghighlight包突出高亮条形图中的特定条形(highlight specific bar plot)

我可以使用 plotly 子图在一个图形对象中显示不同的图表类型,例如雷达、旭日形、甜甜圈吗