是否可以使用ggvis以交互方式更改x和y轴的变量?

时间:2014-07-19 13:15:31

标签: r ggplot2 statistics ggvis

有没有人知道是否可以用ggvis以交互方式更改x和y轴的变量?我可以改变数据点的大小,它们的位置和不透明度,但是如果可以允许用户从下拉列表中选择一个变量来成为x / y轴的数据,我就无法解决。

5 个答案:

答案 0 :(得分:16)

你可以这样做:

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))

答案 1 :(得分:8)

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

下面我将说明如何使用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 = Petal.Width

使用input_select这可以更改为:

input_select = Sepal.Length

如果您不想使用dplyr / magrittr,它将如下所示:

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

答案 2 :(得分:5)

您目前无法直接在ggvis(v0.3)中执行此操作。来自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")
    )
  )
))

答案 3 :(得分:1)

您还可以将绘图构建为交换轴的闪亮反应函数。当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 :(得分: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进行相同操作。