首页 > 解决方案 > 有没有办法在 R 中使用 facet_grid 和交互式 plot_ly?

问题描述

我正在寻找一些关于如何使用facet_gridsubplot在下面的示例中的指导。所需的输出将是:单个条形图的网格,其下拉菜单由变量“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)))))
      )  
}

标签: rggplot2plotlydata-visualizationr-plotly

解决方案


您可以做两个单独的图并将它们与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)

推荐阅读