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