在R中以绘图方式共享子图之间共享轴和图例(ggplot2中的构面和使用ggplotly不起作用)

时间:2019-06-25 23:32:43

标签: r ggplot2 plotly facet slidify

我有以下数据:

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,则可以选择和取消选择变量,但是条形图不会重新调整,因此我得到的内容如下:

badplot

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

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)

Bad Plotly

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

这可能吗?

(我想将这样的交互式图形嵌入slidify中,如果有更好的库可以使用它们。到目前为止,rCharts使我失败了,所以我要作图)

1 个答案:

答案 0 :(得分:0)

我知道了!不需要最后投放我的数据。我什至还添加了添加子组标题的步骤。

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

Good plot