r绘制与自定义配色栏

发布于 2025-01-18 15:44:31 字数 5195 浏览 4 评论 0原文

我正在尝试使用 plot_ly 重现通过 ggplotly 生成的图表。然而我在颜色条上挣扎。

这是我想要重现的 ggploty 图,特别是颜色条:

library(plotly)

X <- data.frame(w = rep(c("a", "b"), each = 8),
                x = 1:16,
                y = 1:16,
                z = c(1, 1:13, 13, 13))

X$z_scaled <- (X$z-min(X$z))/(max(X$z)-min(X$z)) # scale to 0-1

# ggplot
gg <- ggplot(X) +
  geom_point(aes(x, y, color = z_scaled, alpha = w, text = paste0(x, ", ", y))) +
  scale_color_gradient2(low = '#0d71db', mid = "#dbc00d", high = '#db220d', 
                        midpoint = .5, breaks = 0:1, limits = 0:1) +
  scale_alpha_manual(name = " ", values = rep(1, nrow(X))) +
  labs(color = "Z", x = "", y = "")

ggplotly(gg, type = "scattergl", tooltip = "text") %>% toWebGL()

在此处输入图像描述

这就是我对 plot_ly 的了解:

length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%  
  add_markers(
    data = X, x = ~x, y = ~y,
    split = ~w,
    text = ~paste0(x, ", ", y),
    hoverinfo = "text",
    type = "scattergl",
    mode = "markers",
    marker = list(
      # color = ~z_scaled,
      color = .colors,
      # colorscale = list(c(0, .5, 1), c("#0d71db", "#dbc00d", "#db220d")),
      colorscale = .colors,
      hoverlabel = list(bgcolor = .colors),
      colorbar = list(
        title = list(text = "Z"),
        len = .5,
        x = 1,
        y = .7
      )
    )
  ) %>% 
  layout(
    legend = list(x = 1, y = .4, bgcolor = 'rgba(255,255,255,0.6)')
  ) %>% toWebGL() %>% partial_bundle(local = FALSE)

在此处输入图像描述

如您所见,colorbar 显示不正确。我尝试了多种可能性(上面评论过)但没有成功。我缺少什么?


编辑
@Kat 的答案解决了颜色条问题。但是,如果您想使用 scattergl 或 toWebGl ,您将需要修复悬停标签背景,使其保持动态。以下是基于她的答案的解决方案。

length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scattergl",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d'),
            hoverlabel = list(bgcolor = .colors)) %>% # Fix hovercolor bg
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25) %>%              # width
           toWebGL() %>% partial_bundle(local = FALSE)

编辑2
悬停标签 bgcolor 崩溃,然后分割因子未排序。这就是为什么需要先订购的原因。

library(plotly)
library(data.table)

X <- data.table(w = rep(c("a", "b"), 8), #not ordered
                x = 1:16,
                y = 1:16,
                z = c(1, 1:13, 13, 13))

X[, z_scaled := (X$z-min(X$z))/(max(X$z)-min(X$z))] # scale to 0-1

# Get colors for hoverlabel bgcolor
X <- X[order(w)]
length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scattergl",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d'),
            hoverlabel = list(bgcolor = .colors),
            marker = list(size = 10)) %>% # Fix hovercolor bg
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25) %>%              # width
           toWebGL() %>% partial_bundle(local = FALSE)

I am trying to reproduce a graph generated via ggplotly with plot_ly. I am struggling however with the colorbar.

This is the ggploty plot that I would like to reproduce, and in particular the colorbar:

library(plotly)

X <- data.frame(w = rep(c("a", "b"), each = 8),
                x = 1:16,
                y = 1:16,
                z = c(1, 1:13, 13, 13))

X$z_scaled <- (X$z-min(X$z))/(max(X$z)-min(X$z)) # scale to 0-1

# ggplot
gg <- ggplot(X) +
  geom_point(aes(x, y, color = z_scaled, alpha = w, text = paste0(x, ", ", y))) +
  scale_color_gradient2(low = '#0d71db', mid = "#dbc00d", high = '#db220d', 
                        midpoint = .5, breaks = 0:1, limits = 0:1) +
  scale_alpha_manual(name = " ", values = rep(1, nrow(X))) +
  labs(color = "Z", x = "", y = "")

ggplotly(gg, type = "scattergl", tooltip = "text") %>% toWebGL()

enter image description here

This is what I have with plot_ly:

length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%  
  add_markers(
    data = X, x = ~x, y = ~y,
    split = ~w,
    text = ~paste0(x, ", ", y),
    hoverinfo = "text",
    type = "scattergl",
    mode = "markers",
    marker = list(
      # color = ~z_scaled,
      color = .colors,
      # colorscale = list(c(0, .5, 1), c("#0d71db", "#dbc00d", "#db220d")),
      colorscale = .colors,
      hoverlabel = list(bgcolor = .colors),
      colorbar = list(
        title = list(text = "Z"),
        len = .5,
        x = 1,
        y = .7
      )
    )
  ) %>% 
  layout(
    legend = list(x = 1, y = .4, bgcolor = 'rgba(255,255,255,0.6)')
  ) %>% toWebGL() %>% partial_bundle(local = FALSE)

enter image description here

As you can see, the colorbar is not displaying correctly. I have tried multiple possibilities (commented above) without success. What am I missing?


Edit
@Kat's answer solves the colorbar issue. However, if you want to use scattergl or toWebGl you will need to fix the hoverlabel background so it remains dynamic. Here is a solution for that below building on her answer.

length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scattergl",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d'),
            hoverlabel = list(bgcolor = .colors)) %>% # Fix hovercolor bg
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25) %>%              # width
           toWebGL() %>% partial_bundle(local = FALSE)

Edit 2
The hoverlabel bgcolor breaks down then the split factor is not ordered. This is why it needs to be ordered first.

library(plotly)
library(data.table)

X <- data.table(w = rep(c("a", "b"), 8), #not ordered
                x = 1:16,
                y = 1:16,
                z = c(1, 1:13, 13, 13))

X[, z_scaled := (X$z-min(X$z))/(max(X$z)-min(X$z))] # scale to 0-1

# Get colors for hoverlabel bgcolor
X <- X[order(w)]
length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scattergl",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d'),
            hoverlabel = list(bgcolor = .colors),
            marker = list(size = 10)) %>% # Fix hovercolor bg
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25) %>%              # width
           toWebGL() %>% partial_bundle(local = FALSE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

撩人痒 2025-01-25 15:44:32

假设您甚至想要灰色背景,这应该可行。如果有些内容不清楚或不是您想要的内容,请告诉我。

您甚至不需要文本,因为您指定了默认的悬停内容。

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scatter",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d')) %>% 
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25)                  # width

输入图片此处描述

在此处输入图像描述

Assuming you even wanted the gray background, this should work. If something isn't clear or not what you were looking for, let me know.

You don't even need the text, because you specified the default hover content.

plot_ly() %>%
  add_trace(x = ~x, 
            y = ~y, 
            split = ~w,          # instead of alpha or opacity
            data = X,
            type = "scatter",
            mode = "markers",
            color = ~z_scaled,   # color = var and colors = literal colors
            colors = c('#0d71db', "#dbc00d", '#db220d')) %>% 
  layout(xaxis = list(title = "",
                      dtick = 4,
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         yaxis = list(title = "",
                      dtick = 4, 
                      zeroline = F,
                      gridcolor = "white"), # white on gray
         plot_bgcolor = "#eeeeee") %>%      # gray background
  colorbar(title = "Z",                     # colorbar title
           dtick = c(0, 1),                 # colorbar ticks
           thickness = 25)                  # width

enter image description here

enter image description here

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