【问题标题】:Sharing axes and legends between subplots in plotly in R (faceting in ggplot2 and using ggplotly doesn't work)在 R 中的 plotly 中的子图之间共享轴和图例(在 ggplot2 中分面并使用 ggplotly 不起作用)
【发布时间】:2019-06-25 23:32:43
【问题描述】:

我有以下数据:

df <- data.frame(numbers = rep(1:3, 30),
                 letter = sample(c("A", "B", "C", "D"), 90, replace = TRUE),
                 status = sample(c("good", "bad", "ugly"), 90, replace = TRUE))

我正在尝试复制这个 ggplot2 图,但让它具有交互性:

ggplot(df, aes(letter, fill = status)) + geom_bar() + facet_wrap(.~numbers)

如果我使用ggplotly,那么我可以选择和取消选择变量,但条形不会重新调整,所以我得到如下所示的内容:

所以我的想法是转换数据,然后创建单独的情节图并使用子图:

df_group <- df %>% group_by(numbers, letter, status) %>% tally()
df_group_cast <- dcast(df_group, numbers + letter ~ status)

p1 <- df_group_cast %>% 
    filter(numbers == 1) %>%
    plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
    add_trace(y = ~bad, name = 'bad') %>%
    add_trace(y = ~ugly, name = 'ugly') %>%
    layout(yaxis = list(title = 'Count'), barmode = 'stack')

p2 <- df_group_cast %>% 
    filter(numbers == 2) %>%
    plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
    add_trace(y = ~bad, name = 'bad') %>%
    add_trace(y = ~ugly, name = 'ugly') %>%
    layout(yaxis = list(title = 'Count'), barmode = 'stack')

p3 <- df_group_cast %>% 
    filter(numbers == 3) %>%
    plot_ly(x = ~letter, y = ~good, type = 'bar', name = 'good') %>%
    add_trace(y = ~bad, name = 'bad') %>%
    add_trace(y = ~ugly, name = 'ugly') %>%
    layout(yaxis = list(title = 'Count'), barmode = 'stack')

subplot(p1, p2, p3)

这是交互式的,但看起来也很糟糕。我希望他们共享一个比例,有一个图例,并为每个数字组指定标题。

这可能吗?

(我正在尝试将这样的交互式图表嵌入到 slidify 中,如果有更好的库我愿意使用它们。到目前为止 rCharts 让我失望了,所以我正在尝试)

【问题讨论】:

    标签: r ggplot2 plotly facet slidify


    【解决方案1】:

    我想通了!最后不需要转换我的数据。我什至添加了添加子组标题的步骤。

    df_group <- df %>% group_by(numbers, letter, status) %>% tally()
    

    将注释文本放在一起以添加到绘图中:

    a <- list(
        text = sprintf("<b>1</b>"),
        xref = "paper",
        yref = "paper",
        yanchor = "bottom",
        xanchor = "center",
        align = "center",
        x = 0.5,
        y = 1,
        showarrow = FALSE)
    
    b <- list(
        text = sprintf("<b>2</b>"),
        xref = "paper",
        yref = "paper",
        yanchor = "bottom",
        xanchor = "center",
        align = "center",
        x = 0.5,
        y = 1,
        showarrow = FALSE)
    
    c <- list(
        text = sprintf("<b>3</b>"),
        xref = "paper",
        yref = "paper",
        yanchor = "bottom",
        xanchor = "center",
        align = "center",
        x = 0.5,
        y = 1,
        showarrow = FALSE)
    

    将实际绘图放在一起,注意布局下的“注释”选项。我也不需要添加所有这些废话,按状态着色就可以了。

    p1 <- df_group %>% 
        filter(numbers == 1) %>% 
        group_by(letter) %>% 
        plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status) %>% 
        layout(barmode = 'stack', annotations = a)
    
    p2 <- df_group %>% 
        filter(numbers == 2) %>% 
        group_by(letter) %>% 
        plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status, showlegend = FALSE) %>% 
        layout(barmode = 'stack', annotations = b)
    
    p3 <- df_group %>% 
        filter(numbers == 3) %>% 
        group_by(letter) %>% 
        plot_ly(x = ~letter, y= ~n, color = ~status, type = 'bar', legendgroup = ~status, showlegend = FALSE) %>% 
        layout(barmode = 'stack', annotations = c)
    

    绘图:

    subplot(p1, p2, p3, shareY = TRUE)
    

    Imgur 无法显示交互性,因此您只需要相信这是交互性的,您可以通过单击标签来选择所有图中的类别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-21
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多