R Plotly:使用颜色变量时更改图的颜色

R Plotly: Change colors of plot when color variable is used

我是 plotly 的新手,不知道如何在使用颜色变量时为不同的组设置颜色。我的代码如下所示:

library(plotly)
a <- runif(10)
b <- runif(10)
c <- sample(c("Group1","Group2"),10,replace = T)
plot_ly(x = a, y = b, color = c)

您可以使用 colors 参数选择颜色。

library(plotly)
a <- runif(10)
b <- runif(10)
c <- sample(c("Group1","Group2"),10,replace = T)
plot_ly(x = a, y = b, color = c, colors = c("green","red"))

说明:"color" 参数让您选择应该着色的变量,"colors" 参数让您选择颜色

来自帮助:

color :

A formula containing a name or expression. Values are scaled and mapped to color codes based on the value of colors and alpha. To avoid scaling, wrap with I(), and provide value(s) that can be converted to rgb color codes by grDevices::col2rgb().

colors:

Either a colorbrewer2.org palette name (e.g. "YlOrRd" or "Blues"), or a vector of colors to interpolate in hexadecimal "#RRGGBB" format, or a color interpolation function like colorRamp().