rggvis

R ggvis:use input_select to set variable on x-axis


I am trying to set the x variable in a ggvis plot using input_select. Suppose the following base plot:

mtcars %>% 
  ggvis(~wt, ~mpg) %>%
  layer_points()

I read here that I should use this expression:

prop(x = input_select(c("disp", "wt")), constant = FALSE)

But I don't know how to combine these two pieces of code.


Solution

  • Found it !!!

    xVariables = c("hp","disp")
    
    mtcars %>% 
      ggvis(prop("x", input_select(xVariables, map=as.name), scale = TRUE), y=~mpg) %>%
      scale_numeric("x", domain = c(NA, NA), clamp = TRUE, nice = TRUE) %>%
      scale_numeric("y", domain = c(NA, NA), clamp = TRUE, nice = TRUE) %>%
      add_axis("x", title = "I don't know how this make this dynamic.") %>%
      layer_points()
    

    (Unfortuntaly I could not figure out how to adjust the xlabel dynamically. If anyone knows, complementing would be very welcome.)

    This post from Jonathan H helped a lot!