【问题标题】:Duplicated legends when faceting in ggplotly在ggplotly中刻面时重复的图例
【发布时间】:2016-03-28 02:51:18
【问题描述】:

我正在用ggplotly() 制作一些数字,并注意到facet_wrapfacet_grid 会导致图例中的每个项目都按面数重复。有没有办法阻止这种情况?

例如:

library("ggplot2")
library("plotly")
diamonds = diamonds[diamonds$cut %in% c("Fair", "Good"),]
dia = ggplot(diamonds, aes(x = cut)) + 
  geom_bar(aes(stat = "identity", fill = cut)) + 
  facet_grid(.~color)

ggplotly(dia)

?plotly 文档不是很详细,these 都没有图例。

当我输入 ggplotly 时会出现以下情况:

function (p = ggplot2::last_plot(), filename, fileopt, world_readable = TRUE) 
{
    l <- gg2list(p)
    if (!missing(filename)) 
        l$filename <- filename
    if (!missing(fileopt)) 
        l$fileopt <- fileopt
    l$world_readable <- world_readable
    hash_plot(p$data, l)
}

【问题讨论】:

  • 我认为这是按预期工作的。见Coordinating Color Across Subplots。看起来颜色映射将应用于每个 x 轴。在这种特定情况下,不需要刻面。一个更严重的错误是 Plotly 图表的 y 值与 ggplot 图表中的不同。但是对于 geom_point 类型,它们是相同的。
  • @VanceLopez 是的,我明白你的意思。所以现在我同意它“正常”工作,但对于像我的例子这样的情况绝对不是想法(我同意这不是必需的,但只是可以使用的东西)。
  • @VanceLopez 我什至没有注意到 plotly 和 ggplot y 值之间的差异。不错的收获。但是怎么办?

标签: r ggplot2 plotly


【解决方案1】:

更新

Plotly 3.6.0 修复了一些问题——2016 年 5 月 16 日

由于 geom_bar 的 ggplotly 错误会扭曲条形数据,因此可能没有一个好的方法来执行此操作。对于这种特殊情况,不需要 facet。您可以使用 plot_ly() 构建一个有效的情节。

Plot_ly

require(plotly)
require(dplyr)

d <- diamonds[diamonds$cut %in% c("Fair", "Good"),] %>%
  count(cut, color)

plot_ly(d, x = color, y = n, type = "bar", group = cut)

使用 Plotly subplot()

如果这种情节类型是必须的,您可以使用 Plotly 的子情节构建一个类似平面的情节。不好看。

d2 <- diamonds[diamonds$cut %in% c("Fair", "Good"),] %>%
  count(cut, color) %>%
  transform(color = factor(color, levels=rev(levels(color)))) %>%
  mutate(id = as.integer(color)) 

p <- plot_ly(d2, x = cut, y = n, type = "bar", group = color, xaxis = paste0("x", id), marker = list(color = c("#0000FF","#FF0000"))) %>%
  layout(yaxis = list(range = range(n), linewidth = 0, showticklabels = F, showgrid = T, title = ""),
         xaxis = list(title = ""))

subplot(p) %>%
  layout(showlegend = F,
         margin = list(r = 100),
         yaxis = list(showticklabels = T),
         annotations = list(list(text = "Fair", showarrow = F, x = 1.1, y = 1, xref = "paper", yref = "paper"),
                            list(text = "Good", showarrow = F, x = 1.1, y = 0.96, xref = "paper", yref = "paper")),
         shapes = list(list(type = "rect", x0 = 1.1, x1 = 1.13, y0 = 1, y1 = 0.97, line = list(width = 0), fillcolor = "#0000FF", xref = "paper", yref = "paper"),
                       list(type = "rect", x0 = 1.1, x1 = 1.13, y0 = 0.96, y1 = 0.93, line = list(width = 0), fillcolor = "#FF0000", xref = "paper", yref = "paper")))

【讨论】:

    【解决方案2】:

    在这种情况下,您可以关闭指南/图例,因为您并不需要它。

    library("ggplot2")
    library("plotly")
    diamonds = diamonds[diamonds$cut %in% c("Fair", "Good"),]
    dia = ggplot(diamonds, aes(x = cut)) + 
      geom_bar(aes(stat = "identity", fill = cut)) + 
      guides(fill=FALSE) +
      facet_grid(.~color)
    
    ggplotly(dia)
    

    【讨论】:

    • 这绝对是一个选择,但在更复杂(或正式)的设置中,我需要一个图例。这是一种奇怪的行为!
    猜你喜欢
    • 2021-11-01
    • 2020-02-25
    • 2011-11-08
    • 2017-12-24
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多