【问题标题】:Is it possible with ggvis to interactively change the variables for the x and y axes?ggvis 是否可以交互更改 x 和 y 轴的变量?
【发布时间】:2014-09-10 12:35:08
【问题描述】:

有谁知道是否可以使用 ggvis 交互地更改 x 和 y 轴的变量?我可以更改数据点的大小、位置和不透明度,但我无法确定是否允许用户从下拉列表中选择一个变量,该变量将成为 x/y 轴的数据。

【问题讨论】:

标签: r ggplot2 statistics ggvis


【解决方案1】:

是的。你可以这样做:

library(ggvis)

mtcars %>% 
  ggvis(x = ~mpg, y = input_select(label = "Choose what to plot:",
                                   choices = names(mtcars),
                                   selected = "cyl",
                                   map = as.name)) %>% 
  layer_points()

如果您想同时选择这两个变量,只需对 x 执行相同操作即可。

【讨论】:

    【解决方案2】:

    ggvis 包旨在与dplyr 结合使用,例如总结数据。 dplyr 包还重新导出了 magrittr 管道运算符(%>%,请参见 README.md),这使得使用 ggvis' 实现 The Grammar of Graphics 特别直观(另请参见作者的 this article这些包中,Hadley Wickham)。

    下面我将说明如何使用input_select() 函数来更改模型的x 并保持y 不变。

    首先我们需要加载两个必需的库:

    library(dplyr)
    library(ggvis)
    

    现在我们可以绘制一个data.frame(我使用的是内置预加载的iris):

    iris %>% 
      ggvis(x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name)) %>% 
      layer_points(y = ~Petal.Length, fill = ~Species)
    

    输出是:

    使用input_select 可以更改为:

    如果您不想使用dplyr / magrittr,它看起来像这样:

    p <- ggvis(iris, x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name))
    layer_points(p, y = ~Petal.Length, fill=~Species)
    

    【讨论】:

    • 当时这是最接近的事情,现在下面的答案更好。我仍然不明白你为什么会投反对票
    【解决方案3】:

    您还可以将绘图构建为交换轴的闪亮反应函数。 ggvis重绘情节时可能会有闪光,但会产生你想要的效果。

    这会修改上面ideamotor的代码;我还修改了它,使用响应函数而不是响应数据作为 ggvis 的输入,这允许 ggvis ......哦,试试吧,你会看到:

    library(shiny);library(ggvis)
    shinyServer(function(input, output) {
      plotData <- reactive({
        df <- iris[,c("Sepal.Width",input$yVariable,"Species")]
        names(df) <- c("x","y","fill")
        df
      })
      reactive({ 
            plt <- **plotData** %>%  ggvis(fill=~fill) %>%
                   add_legend("fill", title = "Species")
            if (**input$someCheckBox**) {
                   plt <- plt %>%
                   layer_points(x = ~x, y = ~y) %>%
                   add_axis("x", title = "Sepal.Width") %>%
                   add_axis("y", title = input$yVariable)
                } else {
                   plt <- plt %>% 
                   layer_points(x = ~y, y = ~x) %>%
                   add_axis("y", title = "Sepal.Width") %>%
                   add_axis("x", title = input$yVariable)                  
                } 
            plt
      }) %>%  bind_shiny("ggvisPlot")
    })
    

    【讨论】:

      【解决方案4】:

      你可以这样做:

      library('ggvis');
      mtcars %>% ggvis(~mpg, input_select(names(mtcars), map = as.name)) %>% layer_lines()
      # or specify by hand
      mtcars %>% ggvis(~mpg, input_select(c('wt', 'disp'), map = as.name)) %>% layer_lines()
      

      (关键是使用 map 和合适的函数,在这种情况下 as.name() 可以,但如果您有特殊需要,您可以创建自己的)

      请参阅有关 input_select 的文档:http://www.rdocumentation.org/packages/ggvis/functions/input_select

      interactivity 的文档在描述闪亮解决方案的答案中引用(嗯,我需要信誉点来发布超过 2 个链接,所以我不能这样做,但链接已经在里面了! ) 表示这是可能的(与该答案所述相反)但那里提供的语法不起作用

      prop(x = input_select(c("disp", "wt")), constant = FALSE)
      # which is to be used with props:
      props(prop(x = input_select(c("disp", "wt")), constant = FALSE))
      

      但是有一些使用 as.name (http://ggvis.rstudio.com/properties-scales.html) 的提示:

      var <- "mpg"
      prop("x", as.name(var))
      

      【讨论】:

      • 或者,将列转换为行并用不同的颜色将它们绘制在一起(这可能不适用于某些情况,例如当有很多列时),在这里我将 vs 和 am 组合成一个数字列 val,并添加另一列 new.field,它记录 val 下的值是哪种类型。然后我使用填充为每种类型设置不同的颜色: df1 % ggvis(~mpg, ~val, fill=~factor(new.field)) %>% layer_points()
      • 是否可以同时对 x 轴和 y 轴执行此操作?
      【解决方案5】:

      目前(v0.3)您不能直接在ggvis 中执行此操作。来自documentation

      Currently, interactive inputs can only be used in two places:
      
      1. as arguments to transforms: layer_smooths(span = input_slider(0, 1))
      2. as properties: props(size = input_slider(10, 1000))
      
      This means that interactive inputs can only modify the data, not the underlying plot specification. 
      In other words, with only basic interactivity there’s no way to add or remove layers, or switch between different datasets. 
      This is a reasonable limitation because if you’re doing exploration you can always create a new ggvis with R code, or if you’re polishing a plot for presentation, you can embed it in a Shiny app and gain full control over the plot.
      

      因此解决方案是使用shiny 并为变量提供输入并以反应方式定义数据集。这是你的服务器。R:

      library(shiny);library(ggvis)
      shinyServer(function(input, output) {
        plotData <- reactive({
          df <- iris[,c("Sepal.Width",input$yVariable,"Species")]
          names(df) <- c("x","y","fill")
          df
        })
        reactive({ plotData() %>%  ggvis(x=~x,y=~y,fill=~fill) %>%
                     layer_points() %>%
                     add_axis("x", title = "Sepal.Width") %>%
                     add_axis("y", title = input$yVariable) %>%
                     add_legend("fill", title = "Species")
        }) %>%  bind_shiny("ggvisPlot")
      })
      

      还有你的 ui.R:

      library(shiny);library(ggvis)
      shinyUI(fluidPage(
        titlePanel("ggvis with changing data-set"),
        sidebarLayout(
          sidebarPanel(
            selectInput("yVariable", "Y Variable:",
                        c("Petal.Width" = "Petal.Width",
                          "Petal.Length" = "Petal.Length"),selected = "Petal.Width")
          ),
          mainPanel(
            ggvisOutput("ggvisPlot")
          )
        )
      ))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多