R Plotly中的自定义颜色

R Plotly中的自定义颜色,r,plotly,r-plotly,R,Plotly,R Plotly,我目前是Plotly的初学者,有一个我似乎无法解决的问题。我有我的绘图堆叠条形图,但我不知道如何为每个类别着色。我目前正在使用R 这是我当前的堆叠条形图: 我目前的代码是: p = plot_ly(x, x = day, y = count, type = "bar", group = status) %>% layout(barmode = "stack", showlegend = T) 我尝试过使用“color=“参数和标记,但没有任何东西可以正确地为图形着色。以下是一些可能有用

我目前是Plotly的初学者,有一个我似乎无法解决的问题。我有我的绘图堆叠条形图,但我不知道如何为每个类别着色。我目前正在使用R

这是我当前的堆叠条形图:

我目前的代码是:

p = plot_ly(x, x = day, y = count, type = "bar", group = status) %>% layout(barmode = "stack", showlegend = T)

我尝试过使用
“color=“
参数和标记,但没有任何东西可以正确地为图形着色。

以下是一些可能有用的东西。请注意,由于某种原因,方法1导致图例条目为黑色。因此,我也建议采取变通办法

library(plotly)
library(dplyr)
library(data.table)

mtcars$color <- factor(mtcars$gear, labels = c("blue", "red", "green"))

# Method 1
# Legend entries are all black
plot_ly(mtcars, x = as.factor(cyl), y = mpg, group = gear, type = "bar", marker = list(color = color), name = "test") %>% 
  layout(barmode = "stack", showlegend = T)

# Method 2
# Workaround
dt <- data.table(mtcars)

p <- dt[gear == 3,] %>% 
  plot_ly(x = factor(cyl), y = mpg, name = "Gear = 3", type = "bar", marker = list(color = "blue"))

p <- dt[gear == 4,] %>% 
  add_trace(x = factor(cyl), y = mpg, name = "Gear = 4", type = "bar", marker = list(color = "red"))

p <- dt[gear == 5,] %>% 
  add_trace(x = factor(cyl), y = mpg, name = "Gear = 5", type = "bar", marker = list(color = "green"))

p <- layout(p, barmode = "stack")

p
library(plotly)
图书馆(dplyr)
库(数据表)
mtcars$color%
布局(barmode=“stack”,showlegend=T)
#方法2
#变通办法

dt您需要为
颜色
参数指定一个因子,然后为
颜色
参数指定一个颜色向量

这里有一个简单的解决方案。打印前,请注意数据框上所需的顺序

require(dplyr)
require(plotly)

set.seed(42)

df <- data.frame(x = rep(LETTERS[1:5], 3), 
                 y = rexp(15, rate = 0.5),
                 z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
df <- arrange(df, desc(z))

plot_ly(df, 
        x = x, 
        y = y, 
        color = z, 
        colors = c("grey50", "blue", "red"), 
        type = "bar") %>% 
    layout(barmode = "stack")
require(dplyr)
要求(详细地)
种子(42)

df我今天遇到了这个问题,因为我试图为每个单独的标记定义颜色,并且努力工作。答案来自另一个答案,但那个答案说它对这个问题不起作用,但对我的问题起作用

以下是关于和MRE的要点:


本质上,您使用的不是
color
和/或
colors
,而是
marker=list(color=~color)
。这假设您有像我这样的数据,其中每个点都有一种颜色。这对我来说至关重要,因为当我使用
color=~Hex,colors=~Hex
时,使用了颜色,但数据被排序,颜色与正确的标记不匹配

下面是对plot_\ly语句的一些修改(~,和~factor()),以使输出与plotly的较新版本一起工作

require(dplyr)
require(plotly)

set.seed(42)

df <- data.frame(x = rep(LETTERS[1:5], 3), 
                 y = rexp(15, rate = 0.5),
                 z = c(rep("Adam", 5), rep("Arthur", 5), rep("Ford", 5)))
df <- arrange(df, desc(z))

plot_ly(df, 
        x = ~x, 
        y = ~y, 
        color = ~factor(z), 
        colors = c("grey50", "blue", "red"), 
        type = "bar") %>% 
  layout(barmode = "stack")
require(dplyr)
要求(详细地)
种子(42)

df“我尝试过使用“color=”参数和标记,但没有正确地为我的图形着色”。。。你尝试了什么?你所说的“我的图表没有正确的颜色”是什么意思。我们无法测试您的代码,我们无法访问您的数据集不确定答案是否清楚,但您需要在数据框中的一列中存储实际颜色代码(或类似颜色代码)。然后使用
marker=list(color=~column\u with\u color)
color
colors
plotly选项一起删除。感谢bhive01,这基本上解决了我的问题。我现在唯一的问题是没有显示任何图例。有人知道添加自定义彩色图例的方法吗?