I have XY
data which I'd like to graph using a scatter
plot
, with R
's plotly
package.
set.seed(1)
df <- data.frame(x=c(rnorm(30,1,1),rnorm(30,5,1),rnorm(30,9,1)),
y=c(rnorm(30,1,1),rnorm(30,5,1),rnorm(30,9,1)),
group=c(rep("A",30),rep("B",30),rep("C",30)),score=runif(90,0,1))
Each point is assigned to one of three groups (df$group
) and has a score in the [0,1]
range.
I'm looking for a way to graph the data such that each group is colored with a different color but the shade of that color (or intensity) reflects the score.
So I thought this would work:
library(dplyr)
library(plotly)
plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$score,colors=c("#66C2A5","#FC8D62","#8DA0CB")) %>%
layout(xaxis=list(title="X",zeroline=F,showticklabels=F),yaxis=list(title="Y",zeroline=F,showticklabels=F))
If I just color code by group
:
plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$group,colors=c("#66C2A5","#FC8D62","#8DA0CB")) %>%
layout(xaxis=list(title="X",zeroline=F,showticklabels=F),yaxis=list(title="Y",zeroline=F,showticklabels=F))
So it looks like it is mixing the group
colors and the score
gradient.
What I'm looking for is to have the bottom left group colored in shades of green (say from gray
to darkgreen
) that correspond to score
(low to high), and the same for the two other groups in orange and blue, respectively.