Is it possible with ggvis to interactively change the variables for the x and y axes?

独自空忆成欢 提交于 2019-11-29 12:09:44

问题


Does anyone know if it is possible to change the variables for the x and y axis interactively with ggvis? I can change the size of the data points, their position and opacity, but I can't work out if its possible to allow the user to select a variable from a dropdown that will become the data for the x/y axis.


回答1:


The ggvis package was designed to be used in conjunction with dplyr, e.g. to summarise data. The dplyr package also re-exports the magrittr pipe operator (%>%, see README.md), which makes working with ggvis' implementation of The Grammar of Graphics particularly intuitive (see also this article by the author of these packages, Hadley Wickham).

Below I'll illustrate how to use the input_select() function to change the x of a model and leave the y constant.

First we need to load the two required libraries:

library(dplyr)
library(ggvis)

Now we can plot a data.frame (I'm using the build-in pre-loaded iris):

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

The output is:

using the input_select this can be changed to:

If you prefer not to use dplyr / magrittr it would look like this:

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



回答2:


You can do that like this:

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

(the key is to use map and a suitable function, in this case as.name() does it but you can create your own if you have special needs)

See documentation for input_select: http://www.rdocumentation.org/packages/ggvis/functions/input_select

The documentation for interactivity referenced in the answer describing the shiny solution (well, I need reputation points to post more than 2 links so I can't do it but the link is given in there!) indicates that this is possible (contrary to what that answer states) but the syntax provided there doesn't work:

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

However there are hints to the use of as.name (http://ggvis.rstudio.com/properties-scales.html):

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



回答3:


You can't do this directly in ggvis currently (v0.3). From the 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.

So the solution is to use shiny and to have inputs for the variables and reactively define the data-set. Here's your server.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")
})

and your 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")
    )
  )
))



回答4:


You could also build the plot into a shiny reactive function that swaps the axes. There may be a flash when ggvis redraws the plot, but it will have the effect you're looking for.

This modifies the code from ideamotor, above; I also altered it to use the reactive function rather than reactive data as the input to ggvis, which allows ggvis to ... oh, just try it, you'll see:

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



回答5:


Yes. You could do as follows:

library(ggvis)

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

If you want to select both variable just do the same for x.



来源:https://stackoverflow.com/questions/24840804/is-it-possible-with-ggvis-to-interactively-change-the-variables-for-the-x-and-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!