您需要为color
参数指定一个因子,然后为参数指定颜色向量colors
。
这是一个简单的解决方案。绘制之前,请注意数据框上所需的排序。
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")
数据帧上的排序很奇怪。我本以为plot_ly
会使用级别的顺序,但不会。
编辑:此示例使用plotly
3.xx如果您使用plotly
4.xx或更高版本,则此代码可能无法正常工作。详情请参阅此处:https://www.r-bloggers.com/upgrading-to-plotly-4-0-and-above/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句