r - 在 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)
如果我使用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 让我失望了,所以我正在尝试)
解决方案
我想到了!最后不需要转换我的数据。我什至添加了添加子组标题的步骤。
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 无法显示交互性,因此您只需要相信这是交互性的,并且您可以通过单击它们的标签来选择所有图中的类别。
推荐阅读
- php - 如何将命名的 PHP $_GET 参数转换为数字索引数组?
- python-3.x - Python3.7与Tensorflow不兼容?
- java - executorService多线程池的性能
- c# - 如何授权 gmail 使用 botframework 网络频道阅读我的收件箱
- r - 获取 SVM 中的特征重要性
- python - Python正则表达式集合中的0个或多个单词
- reactjs - React Native 外包功能
- android - 我需要将 Android textInput 限制为仅 ISO-8859-1 字符
- html - IE11 无法正确显示谷歌字体(仅波兰语标志)
- r - 如何将 CSS 样式应用于模块化闪亮代码?