Search code examples
coordinatestransformggvis

ggvis input_select on scale_numeric trans parameter



I'm really newbie to ggvis and I searched all over web without success.
I have a dataset like this one:

data.example=data.frame("V1"=c(rep("A",times=10),rep("B",times=10)),"V2"=c(runif(10,1,10000),runif(10,100,1000)))

head(data.example)
     V1       V2
    1  A 7261.076
    2  A 1955.755
    3  A 3473.473
    4  A 7593.278
    5  A 3218.867
    6  A 4997.951

tail(data.example)
V1       V2
15  B 721.2147
16  B 185.6676
17  B 939.3541
18  B 276.0682
19  B 910.0322
20  B 444.2188

I can do boxplot with linear scale in y

data.example %>%
  ggvis(x=~V1,y=~V2,fill=~V1) %>%
  layer_boxplots() %>%
  scale_numeric("y",trans="linear")

and a box plot with log scale in y

data.example %>%
  ggvis(x=~V1,y=~V2,fill=~V1) %>%
  layer_boxplots() %>%
  scale_numeric("y",trans="log")

I can't be able to do a dynamic box plot with coordinate transformation selection with code like this:

select.trans=input_select(choices = c("Linear"="linear","Logarithmic"="log"),selected="linear",label="Scale")
data.example %>%
  ggvis(x=~V1,y=~V2,fill=~V1) %>%
  layer_boxplots() %>%
  scale_numeric("y",trans=select.trans,expand=0)

with this error message:

Error in match(x, table, nomatch = 0L) : 
  'match' requires vectorial arguments

May be I ignored some essential ggvis things?
Thanks


Solution

  • According to the ggvis documentation it's only possible to modify the data of a plot with interactive controls (e.g. props) and not the underlying plot specification (e.g. scales).

    It's possible to use Shiny to achieve what you're trying to do though, for example:

    library(shiny)
    library(ggvis)
    
    
    data.example=data.frame(
        "V1"=c(rep("A",times=10),rep("B",times=10)),
        "V2"=c(runif(10,1,10000),runif(10,100,1000))
    )
    
    
    shinyApp(
        ui = fluidPage(
            selectInput("trans", label="Scale",
                        choices=list(linear="linear", log="log"),
                        selected="linear"),
            ggvisOutput("plot")
        ),
        server = function(input, output) {
            reactive({
                data.example %>%
                    ggvis(x=~V1,y=~V2,fill=~V1) %>%
                    layer_boxplots() %>%
                    scale_numeric("y", trans=input$trans, expand=0)
            }) %>% bind_shiny("plot")
        }
    )