0

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)
0

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.