简体   繁体   English

如何在 883276440288 R Barplot 中设置离散 Colors

[英]How to set discrete Colors in Plotly R Barplot

Im trying to replicate a barplot (See picture below) in a Shiny app using Plotly in R.我正在尝试使用 R 中的 Plotly 在 Shiny 应用程序中复制条形图(见下图)。 在此处输入图像描述 The x-axis has tissue values, while each bar on the x-axis is split/stacked based on tissue details. x 轴具有组织值,而 x 轴上的每个条根据组织细节拆分/堆叠。 When I try to replicate the above graph, For some tissue values, I am unable to discretely differentiate between tissue details using the predefined plotly color scales (See 'Brain' and 'heart' below), The bars basically have variations of the same color family, making them hard to interpret.当我尝试复制上图时,对于某些组织值,我无法使用预定义的 plotly 色标(参见下面的“大脑”和“心脏”)来离散区分组织细节,这些条基本上具有相同颜色的变化家庭,使他们难以解释。 在此处输入图像描述

Is it possible somehow to produce a graph with colors like the one I want to replicate, in plotly R?是否有可能以某种方式生成一个 colors 的图形,就像我想在 plotly R 中复制的那样? Dynamically?动态地? As the tissue details for a tissue could change based on shiny select input selections.由于组织的组织详细信息可能会根据 shiny select 输入选择而改变。

This is my plotly function:这是我的 plotly function:

plot_ly(data = tissue_dt, y=~counts, x=tissue, type='bar', name=~tissue_detail, color=~tissue_detail, colors = "Paired") %>% layout(barmode ="stack")

Apart from this I have also tried "Set3" and all other plotly discrete colorscales, but to no luck.除此之外,我还尝试了“Set3”和所有其他 plotly 离散色阶,但没有成功。

The data being used for both the desired plot and the shiny plot, is the same用于所需 plot 和 shiny plot 的数据是相同的

Note that the original plot uses a discrete color for each tissue/detail.请注意,原始 plot 为每个组织/细节使用了离散颜色。

The best option, if the list of the tissue_detail is fixed is to use a manual palette like:如果 tissue_detail 的列表是固定的,最好的选择是使用手动调色板,例如:

my.pal = {
"Adipose-subcutaneous" = "#a10335", #Or whatever color you want
"Adipose-visceral" = "#43a332",
"AdrenalGland" = "green2",
# AND the remaining categories. The labels should match those in tissue_detail
 
}

And the just use that palette in your plot只需在您的 plot 中使用该调色板

plot_ly(data = tissue_dt, y=~counts, x=tissue, type='bar', name=~tissue_detail, color=~tissue_detail, colors = my.pal) %>% layout(barmode ="stack")

This have an additional advantage if you use these data partially in other plots.如果您在其他图中部分使用这些数据,这还有一个额外的优势。 Thus you can keep the assignation of colors to each tissue_detail even if there is some missing category in any of those plots.因此,您可以保留对每个 tissue_detail 的 colors 的分配,即使这些图中的任何一个都缺少一些类别。

With so many (sub) categories it will be rather difficult to define a meaningful discrete palette.有这么多(子)类别,定义一个有意义的离散调色板将相当困难。 However, with plotly's hover capabilities and by adding a (white) line around the bars, you should get a farily decent graph:但是,借助plotly's hover 功能并在条形图周围添加一条(白)线,您应该会得到一个相当不错的图表:

library(dplyr)
library(tibble)
library(plotly)
library(RColorBrewer)

tissues <- tibble(tissue = LETTERS)


set.seed(18012023)
tissue_dt <- tissues %>%
  slice(rep(sample(26, 8), sample(14, 8, TRUE))) %>% 
  mutate(tissue_detail = paste(tissue, sample(letters, n(), TRUE))) %>% 
  right_join(tissues, "tissue") %>% 
  mutate(tissue_detail = coalesce(tissue_detail, tissue),
         counts = rpois(n(), 200)) %>% 
  arrange(tissue)

plot_ly(data = tissue_dt, y = ~counts, x = ~ tissue, 
        type = "bar",
        name = ~tissue_detail, 
        marker = list(line = list(color = "white",
                                  width = 1.5)),
        color = ~tissue_detail, 
        colors = ~ tissue_detail %>% 
          as.factor() %>% 
          nlevels() %>% 
          colorRampPalette(brewer.pal(9, "Set1"))(.)) %>% 
  layout(barmode ="stack")

带有虚拟数据的堆叠条形图显示了白色条如何帮助提高单独条的可读性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM