关闭时,有条件地在图例中错误地渲染段中的段

Posted

技术标签:

【中文标题】关闭时,有条件地在图例中错误地渲染段中的段【英文标题】:Conditionally coloring segments in plotly incorrectly rendering in legend when turned off 【发布时间】:2022-01-14 11:56:56 【问题描述】:

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

## 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,但是当我这样做时,图例会在打开颜色时出现,但在关闭颜色时会完全消失。

【问题讨论】:

【参考方案1】:

您需要通过layout(showlegend = T) 为您的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)

【讨论】:

以上是关于关闭时,有条件地在图例中错误地渲染段中的段的主要内容,如果未能解决你的问题,请参考以下文章

第六章 包含多个段的程序

实验五

实验五

BigQuery 代码段中的错误

实验五

在离子段中获取动态数据时如何删除空列?