有没有办法在R中将facet_grid与交互式plot_ly一起使用? [英] Is there a way to use facet_grid with interactive plot_ly in R?
问题描述
在下面的示例中,我正在寻找有关如何使用 facet_grid
或 subplot
的一些指导.所需的输出将是:单个条形图的网格,其下拉式菜单的变量为"prac".我尝试了 subplot()
和 facet_grid
,但尚未创建适当的输出.以下是我尝试合并 facet_grid()
的方法.有提示吗?
I am looking for some guidance on how to use either facet_grid
or subplot
in my example below. The desired output would be: a grid of individual bar graphs that have the dropdown faceted by the variable "prac". I tried both subplot()
and facet_grid
but haven't yet gotten either to create the appropriate output. Below is how I tried to incorporate the facet_grid()
. Any tips?
library(tidyverse)
library(plotly)
period <- c('201901', '201901', '201904', '201905')
spec <- c('alpha', 'bravo','bravo', 'charlie')
prac <- c('mine','yours','yours','mine')
c <- c(5,6,3,8)
e <- c(1,2,4,5)
df <- data.frame(period, spec,prac, c,e)
spec.val <- unique(df$spec)
ggplot(plot_ly(
df %>% pivot_longer(-c(period, spec, prac)),
x = ~period, y = ~value, color = ~name,
type = "bar",
transforms = list(
list(
type = "filter",
target = ~spec,
operation = "=",
value = spec.val[1]))) %>%
layout(
updatemenus = list(
list(
type = "drowdown",
active = 0,
buttons = map(spec.val, ~list(
method = "restyle",
args = list("transforms[0].value", .x),
label = .x))))) + facet_grid(~prac))
我试图添加一个循环来创建多个图.这不起作用,但是从概念上讲,我认为这可以解决我的问题.关于我在做什么错的任何想法吗?以下内容不会产生任何输出(或错误).
I tried to add a loop to create the multiple plots. This doesnt work, but conceptually I think this could solve my problem. Any ideas on what I am doing wrong? The below doesn't generate any output (or errors).
spec.val <- unique(df$spec)
for (p in unique(df$prac)) {
x <- subset(df, prac == p)
(plot_ly(
df %>% pivot_longer(-c(period, spec, prac)),
x = ~period, y = ~value, color = ~name,
type = "bar",
transforms = list(
list(
type = "filter",
target = ~spec,
operation = "=",
value = spec.val[1]))) %>%
layout(
updatemenus = list(
list(
type = "drowdown",
active = 0,
buttons = map(spec.val, ~list(
method = "restyle",
args = list("transforms[0].value", .x),
label = .x)))))
)
}
推荐答案
您可以制作两个单独的图,并将它们与 subplot
组合:
You could do two separate plots and combine them with subplot
:
library(tidyverse)
library(plotly)
period <- c('201901', '201901', '201904', '201905')
spec <- c('alpha', 'bravo','bravo', 'charlie')
prac <- c('mine','yours','yours','mine')
c <- c(5,6,3,8)
e <- c(1,2,4,5)
df <- data.frame(period, spec,prac, c,e)
spec.val <- unique(df$spec)
df.m <- dplyr::filter(df, prac=="mine") %>% pivot_longer(-c(period, spec, prac))
df.y <- dplyr::filter(df, prac=="yours") %>% pivot_longer(-c(period, spec, prac))
p1 <- plot_ly(
df.m,
x = ~period, y = ~value, color = ~name,
type = "bar",
transforms = list(
list(
type = "filter",
target = ~spec,
operation = "=",
value = spec.val[1]))) %>%
layout(
updatemenus = list(
list(
type = "drowdown",
active = 0,
buttons = map(spec.val, ~list(
method = "restyle",
args = list("transforms[0].value", .x),
label = .x)))))
p2 <- plot_ly(
df.y,
x = ~period, y = ~value, color = ~name,
type = "bar",
transforms = list(
list(
type = "filter",
target = ~spec,
operation = "=",
value = spec.val[1]))) %>%
layout(
updatemenus = list(
list(
type = "drowdown",
active = 0,
buttons = map(spec.val, ~list(
method = "restyle",
args = list("transforms[0].value", .x),
label = .x)))))
subplot(p1, p2)
查看此处以了解更多信息:在r中以plotly绘制的子图
Look here to learn more: Subplot in r with plotly
编辑:更一般而言,您可以生成图表列表并使用子图进行绘制:
More generally, you can generate a list of plots and plot that using subplot:
library(tidyverse)
library(plotly)
period <- c('201901', '201901', '201904', '201905')
spec <- c('alpha', 'bravo','bravo', 'charlie')
prac <- c('mine','yours','yours','mine')
c <- c(5,6,3,8)
e <- c(1,2,4,5)
df <- data.frame(period, spec, prac, c,e)
spec.val <- unique(df$spec)
getPlots <- function(x){
df.m <- dplyr::filter(df, prac==x) %>% pivot_longer(-c(period, spec, prac))
plot_ly(
df.m,
x = ~period, y = ~value, color = ~name,
type = "bar",
transforms = list(
list(
type = "filter",
target = ~spec,
operation = "=",
value = spec.val[1]))) %>%
layout(
updatemenus = list(
list(
type = "drowdown",
active = 0,
buttons = map(spec.val, ~list(
method = "restyle",
args = list("transforms[0].value", .x),
label = .x)))))
}
plotlist <- lapply(levels(df$prac), getPlots)
subplot(plotlist)
这篇关于有没有办法在R中将facet_grid与交互式plot_ly一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!