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

后端 未结 5 1073
日久生厌
日久生厌 2020-12-31 04:46

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

5条回答
  •  佛祖请我去吃肉
    2020-12-31 05:24

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

提交回复
热议问题