设置动态悬停标签背景颜色,r绘图散点

发布于 2025-01-18 18:52:43 字数 2193 浏览 4 评论 0原文

如下示例所示,在使用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"))))

enter image description here

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2025-01-25 18:52:43

您可以使用 RColorBrewer 创建 6 种不同颜色的矢量。您将这些颜色分配给 markercolorhoverlabelbgcolor,这将显示右侧颜色。您可以使用以下代码:

library(plotly)

X <- data.frame(x = 1:6,
                y = 1:6,
                z  = 1:6)

library(RColorBrewer)
cols <- brewer.pal(n = 6, name = "Set3")

plot_ly(data = X, x = ~x, y = ~y,
        type = "scattergl", mode = "markers",
        marker = list(color = cols), 
        hoverlabel = list(bgcolor = cols))

输出:

在此处输入图像描述

正如您从图中看到的,标签与标记的颜色相同。

You can create a vector of 6 different colors using RColorBrewer. These colors you assign to the color of your marker and to the bgcolor of your hoverlabel which will show the right color. You can use the following code:

library(plotly)

X <- data.frame(x = 1:6,
                y = 1:6,
                z  = 1:6)

library(RColorBrewer)
cols <- brewer.pal(n = 6, name = "Set3")

plot_ly(data = X, x = ~x, y = ~y,
        type = "scattergl", mode = "markers",
        marker = list(color = cols), 
        hoverlabel = list(bgcolor = cols))

Output:

enter image description here

As you can see from the plot, the label is the same color as the marker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文