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

时间:2022-09-14 14:59:06

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.

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

5 个解决方案

#1


8  

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

ggvis包被设计成与dplyr一起使用,例如总结数据。 dplyr包还重新导出magrittr管道运算符(%>%,参见README.md),这使得使用ggvis实现的图形语法特别直观(参见这些包的作者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.

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

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

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

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

The output is:

输出是:

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

using the input_select this can be changed to:

使用input_select,可以将其更改为:

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

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

如果您不想使用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


16  

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)

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

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

请参阅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:

在回答中引用交互性的文档描述了闪亮的解决方案(好吧,我需要声望点来发布超过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))

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

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

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

#3


5  

You can't do this directly in ggvis currently (v0.3). From the documentation:

您目前无法直接在ggvis中执行此操作(v0.3)。从文档:

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:

所以解决方案是使用闪亮的,并为变量提供输入并反应性地定义数据集。这是你的服务器.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:

和你的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


1  

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.

您还可以将绘图构建为交换轴的闪亮反应函数。当ggvis重绘绘图时可能会有一个闪光,但它会产生你正在寻找的效果。

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:

这修改了上面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")
})

#5


1  

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.

如果要选择两个变量,只需对x执行相同操作。

#1


8  

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

ggvis包被设计成与dplyr一起使用,例如总结数据。 dplyr包还重新导出magrittr管道运算符(%>%,参见README.md),这使得使用ggvis实现的图形语法特别直观(参见这些包的作者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.

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

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

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

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

The output is:

输出是:

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

using the input_select this can be changed to:

使用input_select,可以将其更改为:

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

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

如果您不想使用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


16  

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)

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

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

请参阅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:

在回答中引用交互性的文档描述了闪亮的解决方案(好吧,我需要声望点来发布超过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))

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

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

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

#3


5  

You can't do this directly in ggvis currently (v0.3). From the documentation:

您目前无法直接在ggvis中执行此操作(v0.3)。从文档:

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:

所以解决方案是使用闪亮的,并为变量提供输入并反应性地定义数据集。这是你的服务器.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:

和你的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


1  

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.

您还可以将绘图构建为交换轴的闪亮反应函数。当ggvis重绘绘图时可能会有一个闪光,但它会产生你正在寻找的效果。

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:

这修改了上面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")
})

#5


1  

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.

如果要选择两个变量,只需对x执行相同操作。