首页 > 解决方案 > 在 R 中的 plotly 中的子图之间共享轴和图例(在 ggplot2 中分面并使用 ggplotly 不起作用)

问题描述

我有以下数据:

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)

ggplot

如果我使用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 让我失望了,所以我正在尝试)

标签: rggplot2plotlyfacetslidify

解决方案


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

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 无法显示交互性,因此您只需要相信这是交互性的,并且您可以通过单击它们的标签来选择所有图中的类别。

好情节


推荐阅读