使用 plotly 时图例中的颜色条 [英] Colorbar in legend when using plotly

查看:148
本文介绍了使用 plotly 时图例中的颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据:

set.seed(42)
mydata = data.frame(A = rnorm(20), B = rnorm(20), Index = sample(190:400,20))    

我试图根据 Index 值将数据划分为 20 个不同的区间,然后根据它们的区间 value 为散点着色.下面是我的代码.它工作不完美.

I am trying to divide the data into 20 different intervals based on the Index value and then color the scatter points according to their interval value. Below is my code. It is not working perfectly.

cols = colorRampPalette(c("red", "black"), space = "rgb")(20)
mydata$interval = cut(mydata$Index,breaks = 20)
mydata$cols = cols[mydata$interval]
require(plotly)
x = list(title = "A")
y = list(title = "B")
plot_ly(mydata, x = ~A, y = ~B,  color = ~cols, type = "scatter",
                        mode = 'markers', hoverinfo = 'text',
                        text = ~paste(interval)) %>%
                        layout(xaxis = x, yaxis = y)

如何在颜色基于的图例中获得 colorbar索引值.

How do I get a colorbar in the legend where the colors are based on Index value.

推荐答案

你在找这个吗:

plot_ly(mydata, x = ~A, y = ~B, type = "scatter",
        mode = 'markers', hoverinfo = 'text', colors = colorRampPalette(c("red", "black"), space = "rgb")(20), color = ~Index, text = ~paste(interval), marker = list(size=14)) %>%
        layout(xaxis = x, yaxis = y) %>%
        colorbar(title = "My Legend")

这篇关于使用 plotly 时图例中的颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆