0
votes

I am trying to modify the labels in the legend of a scatterplot generated in Plotly for R. I am getting 0 and 1 labels. I would like to modify them (0=Bad; 1=Good). Could you be so kind to help me modifying this legend?

I am sharing with you a code that generates this legend and a picture of the chart.

set.seed(1)
x<-rnorm(100)
set.seed(1)
y<-rnorm(100)
set.seed(1)
z<-rdunif(100, 0, 1)
newz<-as.factor(as.numeric(z)) 
scatter<-plot_ly(x = ~x, y = ~y, color = ~newz, type = "scatter", mode="markers")
scatter

Legend

1

1 Answers

1
votes

Recoding newz before or while creating the plot will modify the legend labels.

Here are two ways of recoding while creating the plot:

# recode the color argument
scatter<-plot_ly(x = ~x, y = ~y, color = ifelse(newz == 0, "Bad", "Good"), 
                 type = "scatter", mode="markers") 

# recode the name argument 
scatter<-plot_ly(x = ~x, y = ~y, color = ~newz, type = "scatter", mode="markers",
                 name = ifelse(newz == 0, "Bad", "Good")) 

enter image description here