简体   繁体   English

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

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

I am new to plotly and do not know how to set the colours for the different groups when I use a colour variable. 我是新手,不知道使用颜色变量时如何为不同组设置颜色。 My code looks like this: 我的代码如下所示:

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

you can choose your colors with the colors argument. 您可以使用colors参数选择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"))

explanation: the "color" argument let's you choose your variables which should be colored and the "colors" argument let's you choose your colors 说明:“ color”自变量让您选择应该上色的变量,“ colors”自变量让您选择颜色

from help: 来自帮助:

color : 颜色:

A formula containing a name or expression. 包含名称或表达式的公式。 Values are scaled and mapped to color codes based on the value of colors and alpha. 值会根据颜色和alpha的值进行缩放并映射到颜色代码。 To avoid scaling, wrap with I(), and provide value(s) that can be converted to rgb color codes by grDevices::col2rgb(). 为了避免缩放,请使用I()包装,并提供可以通过grDevices :: col2rgb()转换为rgb颜色代码的值。

colors: 颜色:

Either a colorbrewer2.org palette name (eg "YlOrRd" or "Blues"), or a vector of colors to interpolate in hexadecimal "#RRGGBB" format, or a color interpolation function like colorRamp(). 或者是colorbrewer2.org调色板名称(例如“ YlOrRd”或“ Blues”),或者是要以十六进制“ #RRGGBB”格式进行插值的颜色向量,或者是诸如colorRamp()的颜色插值函数。

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

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