Florian
Florian

Reputation: 61

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)

Upvotes: 0

Views: 2087

Answers (1)

mischva11
mischva11

Reputation: 2956

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

Upvotes: 1

Related Questions