简体   繁体   中英

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.

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

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. 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 (eg "YlOrRd" or "Blues"), or a vector of colors to interpolate in hexadecimal "#RRGGBB" format, or a color interpolation function like colorRamp().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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