简体   繁体   中英

Modify labels in legend Plotly in R

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

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

在此处输入图片说明

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