关闭时在图例中错误地绘制的有条件着色段

Conditionally coloring segments in plotly incorrectly rendering in legend when turned off

我有一个生成绘图的函数,用户可以在其中指定是否根据分组变量为线段着色:

## libraries
library(tidyverse)
library(plotly)

## data
df <- data.frame(grp = c("a", "b"),
                 val_start = c(1, 2),
                 val_end = c(5, 6))

df_long <- df %>% 
  pivot_longer(cols = -grp, names_to = "metric", values_to = "val")

## function
plot_func <- function(plot_color) {
  ## create main plot object
  plot_obj <- df %>% 
    plot_ly()
  ## generate colored/non-colored segments depending on user selection
  if(plot_color == T) {
    plot_obj <- plot_obj %>% 
      add_segments(x = ~val_start,
                   xend = ~val_end,
                   y = ~grp,
                   yend = ~grp,
                   color = ~grp,
                   colors = c("a" = "red", "b" = "blue"))
  } else  {
    plot_obj <- plot_obj %>% 
      add_segments(x = ~val_start,
                   xend = ~val_end,
                   y = ~grp,
                   yend = ~grp)
  }
  ## generate primary colors
  plot_obj %>% 
    add_markers(inherit = F,
                data = df_long,
                x = ~val,
                y = ~grp,
                showlegend = F,
                marker = list(color = "green")) %>% 
    ## generate goal marker
    add_markers(name = "goal",
                x = 4,
                y = ~grp,
                marker = list(color = "black"))
}

如下所示,当用户选择为绘图着色时,该函数可以正常工作:

## render plot
plot_func(plot_color = T)

但是,当用户选择不为绘图着色时,该函数会烦人地为非彩色线条生成图例轨迹,其中 我只想显示目标标记图例.

## render plot
plot_func(plot_color = F)

有谁知道如何解决这个问题?我已经尝试在每个相应的轨迹中指定 showlegend = Tshowlegend = F,但是当我这样做时,图例会在颜色打开时出现,但在颜色关闭时完全消失。

您需要通过 layout(showlegend = TRUE) 为您的 plot_obj“全局”激活 showlegend

## libraries
library(tidyr)
library(plotly)

## data
df <- data.frame(grp = c("a", "b"),
                 val_start = c(1, 2),
                 val_end = c(5, 6))

df_long <- df %>% 
  pivot_longer(cols = -grp, names_to = "metric", values_to = "val")

## function
plot_func <- function(plot_color) {
  ## create main plot object
  plot_obj <- df %>% 
    plot_ly() %>% layout(showlegend = T)
  ## generate colored/non-colored segments depending on user selection
  if(plot_color == T) {
    plot_obj <- plot_obj %>% 
      add_segments(x = ~val_start,
                   xend = ~val_end,
                   y = ~grp,
                   yend = ~grp,
                   color = ~grp,
                   colors = c("a" = "red", "b" = "blue"))
  } else  {
    plot_obj <- plot_obj %>% 
      add_segments(x = ~val_start,
                   xend = ~val_end,
                   y = ~grp,
                   yend = ~grp,
                   showlegend = F)
  }
  ## generate primary colors
  plot_obj %>% 
    add_markers(inherit = F,
                data = df_long,
                x = ~val,
                y = ~grp,
                showlegend = F,
                marker = list(color = "green")) %>% 
    ## generate goal marker
    add_markers(name = "goal",
                x = 4,
                y = ~grp,
                marker = list(color = "black"))
}

plot_func(plot_color = F)