【问题标题】:How to remove duplicate legend entries w/ plotly subplots()如何使用 plotly subplots() 删除重复的图例条目
【发布时间】:2019-12-06 18:33:17
【问题描述】:

使用 plotly 的 subplots() 时如何删除图例中的重复项?

这是我的 MWE:

library(plotly)
library(ggplot2)
library(tidyr)

mpg %>%
  group_by(class) %>%
  do(p = plot_ly(., x = ~cyl, y = ~displ, color = ~trans, type = 'bar')) %>%
  subplot(nrows = 2, shareX = TRUE, titleX = TRUE) %>%
  layout(barmode = 'stack')

【问题讨论】:

  • mpg 是包含在 ggplot2 库中的数据集。不需要 dput()
  • 因为没有class 具有所有不同的trans 级别并且plotly 没有像ggplot2 这样的刻面,我只能想到使用facet_wrapggplot2 然后ggplotly 制作一个只有一个图例的 plotly 对象。如果这对你有用,我可以发布答案。

标签: r ggplot2 plotly subplot r-plotly


【解决方案1】:

plotly 没有 facetggplot2 这样它会为每个 subplot 添加图例,或者您可以为其中一些关闭它。

这里我们没有包含所有~class 条目的层,也没有两个在class 中没有交集的图,它们的组合也涵盖了所有这些图。在这种情况下,我们可以将showlegend 设置为TRUE 用于那些特定的情节,并将其设置为FALSE 用于其余部分,还将legendgroup 设置为trans,这样我们就得到了一个独特但也完整的传说。

正如我所说,这里我们没有那种特殊情况。所以我能想到的有两种可能:

  1. 添加整个数据(复制整个数据框)并将All 类分配给它们。然后将其与原始数据一起绘制,但仅保留 class == All 的图例。

  2. 使用ggplot::facet_wrap,然后使用ggplotly 来创建plotly 对象。但是,这会导致x-axis 出现一些问题(比较ggplot 对象和plotly 对象)。

library(plotly)
library(ggplot2)
library(dplyr)
ly_plot <-  . %>% 
             plot_ly(x = ~cyl, y = ~displ, color = ~trans, 
                     type = 'bar', showlegend = ~all(legendC)) %>%
              add_annotations(
              text = ~unique(class),
              x = 0.5,
              y = 1,
              yref = "paper",
              xref = "paper",
              xanchor = "middle",
              yanchor = "top",
              showarrow = FALSE,
              font = list(size = 15))

mpg %>%
  mutate(class= "_All_") %>% 
  rbind(.,mpg) %>% 
  mutate(legendC = (class == "_All_")) %>% 
  group_by(class) %>%
  do(p = ly_plot(.)) %>%
  subplot(nrows = 2, shareX = TRUE, titleX = TRUE) %>%
  layout(barmode = 'stack')

#> Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, 
#>  allowed maximum for palette Set2 is 8
#> Returning the palette you asked for with that many colors

p <- ggplot(data = mpg, aes(x=cyl, y=displ, fill=trans))+
      geom_bar(stat="identity") +
      facet_wrap(~class)
p  

ggplotly(p) #seems for this we should also set "colour = trans"

【讨论】:

    【解决方案2】:

    使用tidyverse 的另一种解决方法。在原来的 MWE 中增加了以下步骤:

    • trans 列转换为因子。
    • 使用 tidyr 的 complete 填充每个 class 组中缺失因子水平的(非 NA)虚拟值。
    • 按照 M-M 的建议设置 showlegendTRUE 单个组和 legendgrouptrans 以链接子图之间的图例条目。
    library(plotly)
    library(tidyverse)
    
    mpg %>%
        mutate_at("trans", as.factor) %>%  
        group_by(class) %>%
        group_map(.f = ~{          
              ## fill missing levels w/ displ = 0, cyl = first available value 
              complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
              plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
                  showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
              layout(yaxis = list(title = as.character(.y)), barmode = "stack")
            }) %>%
        subplot(nrows = 2, shareX = TRUE, titleY = TRUE) 
    

    【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2010-12-17
    • 2015-06-08
    • 2017-08-04
    • 1970-01-01
    • 2021-08-09
    • 2021-11-01
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多