r - 有没有办法在 R 中使用 facet_grid 和交互式 plot_ly?
问题描述
我正在寻找一些关于如何使用facet_grid
或subplot
在下面的示例中的指导。所需的输出将是:单个条形图的网格,其下拉菜单由变量“prac”组成。 我尝试了两者subplot()
,facet_grid
但还没有得到任何一个来创建适当的输出。下面是我如何尝试将facet_grid()
. 有小费吗?
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))
编辑:
我试图添加一个循环来创建多个图。这不起作用,但从概念上讲,我认为这可以解决我的问题。关于我做错了什么的任何想法?以下不会产生任何输出(或错误)。
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
:
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
编辑:更一般地说,您可以使用子图生成图列表和图:
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)
推荐阅读
- java - 如何在JAVA中链接登录页面和注册页面?(以及如何连接到 MySQL 数据库?)
- linux - 如何更改 ~ 在 Google Cloud Platform 虚拟机上的位置?
- flutter - Flutter SlideTransition 从偏移中心屏幕开始
- django - 带有 HTML 标签的 Django Post 显示
- graphql - 获取 Strapi GraphQL 查询中的图像格式
- python-3.5 - 使用容量和时间窗口约束来解决提货和交付问题
- c# - 在 Visual Studio 2019“解决方案资源管理器”中看不到“参考”文件夹
- java - FileNotFoundException:无法打开类路径资源,因为它不存在
- android - 无需移动即可检测 Android 触摸事件
- azure-active-directory - 如何在 Azure AD 的纯云环境中设置电子邮件属性?