设置动态悬停标签背景颜色,r绘图散点
如下示例所示,在使用scatsgl
而不是scatsgl
时,r绘图中的动态悬臂背景颜色似乎不起作用。
用type =“ sctive”
:
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
plot_ly(data = X, x = ~x, y = ~y,
type = "scatter", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
Hoverlabel背景颜色变成黑色,用于所有数据点,type> type =“ sctategl”
:
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
< a href =“ https://i.sstatic.net/7san3.png” rel =“ nofollow noreferrer”>
我想一个解决方案可能是将与colorscale
中使用的相同颜色数组传递到 bgcolor 通过hoverlabel = list = list(bgcolor = ????)
来参数。但是我不知道该怎么做。
编辑
基于 @Quinten的答案尝试了这一点,但没有成功。可以看出,颜色是默认的plot_ly颜色,并且与cols
中指定的内容无关。
library(plotly)
n <- 5000
X <- data.frame(x = sample(1:100, n, replace = TRUE),
y = sample(1:100, n, replace = TRUE),
z = sample(1:500, n, replace = TRUE))
length_unique_vals <- length(unique(X$z))
cols <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
cols <- cols[factor(X$z)]
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~z,
colorscale = cols,
colorbar = list(title = "Colorbar")),
hoverlabel = list(bgcolor = cols)) %>%
toWebGL()
The dynamic hoverlabel background color in R plotly does not seem to work when using scattergl
instead of scatter
as depicted in the example below.
Works as intended with type = "scatter"
:
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
plot_ly(data = X, x = ~x, y = ~y,
type = "scatter", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
The hoverlabel background color becomes black for all data points with type = "scattergl"
:
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
I guess a solution could be to pass the same color array used in colorscale
to the bgcolor argument via hoverlabel = list(bgcolor = ???)
. However I have no idea how to do so.
Edit
Tried this based on @Quinten's answer, without success. As can be seen the colors are the default plot_ly colors and do not correspond to what is specified in cols
.
library(plotly)
n <- 5000
X <- data.frame(x = sample(1:100, n, replace = TRUE),
y = sample(1:100, n, replace = TRUE),
z = sample(1:500, n, replace = TRUE))
length_unique_vals <- length(unique(X$z))
cols <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
cols <- cols[factor(X$z)]
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~z,
colorscale = cols,
colorbar = list(title = "Colorbar")),
hoverlabel = list(bgcolor = cols)) %>%
toWebGL()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 RColorBrewer 创建 6 种不同颜色的矢量。您将这些颜色分配给
marker
的color
和hoverlabel
的bgcolor
,这将显示右侧颜色。您可以使用以下代码:输出:
正如您从图中看到的,标签与标记的颜色相同。
You can create a vector of 6 different colors using
RColorBrewer
. These colors you assign to thecolor
of yourmarker
and to thebgcolor
of yourhoverlabel
which will show the right color. You can use the following code:Output:
As you can see from the plot, the label is the same color as the marker.