在R中的plotly子地块之间共享轴和图例(在ggplot2中刻面并使用ggplotly不起作用)
我有以下数据:在R中的plotly子地块之间共享轴和图例(在ggplot2中刻面并使用ggplotly不起作用),r,ggplot2,plotly,facet,slidify,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)) 如果我使用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))
如果我使用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)
df_组%group_by(数字、字母、状态)%>%tally()
df_集团_铸造%
plot_ly(x=~字母,y=~好,type='bar',name='good')%>%
添加_跟踪(y=~bad,name='bad')%>%
添加_跟踪(y=~丑陋,name='丑陋')%>%
布局(yaxis=list(title='Count'),barmode='stack')
p2%
过滤器(数字==2)%>%
plot_ly(x=~字母,y=~好,type='bar',name='good')%>%
添加_跟踪(y=~bad,name='bad')%>%
添加_跟踪(y=~丑陋,name='丑陋')%>%
布局(yaxis=list(title='Count'),barmode='stack')
p3%
过滤器(数字==3)%>%
plot_ly(x=~字母,y=~好,type='bar',name='good')%>%
添加_跟踪(y=~bad,name='bad')%>%
添加_跟踪(y=~丑陋,name='丑陋')%>%
布局(yaxis=list(title='Count'),barmode='stack')
子批次(p1、p2、p3)
这是交互式的,但看起来也很糟糕。我希望他们分享一个音阶,有一个传奇,每个数字组都有头衔
这可能吗
(如果有更好的库我愿意使用的话,我正试图在slidify中嵌入一个这样的交互式图形。到目前为止,rCharts让我失望了,所以我正在尝试有计划地)我想出来了!最终不需要转换我的数据。我甚至添加了一个添加子组标题的步骤
df_group <- df %>% group_by(numbers, letter, status) %>% tally()
Imgur无法显示交互性,因此您只需相信这是交互式的,您可以通过单击其标签来选择所有绘图中的类别
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)