0

I have an reactive ggvis scatterplot (layer_points) in shiny. Now i want to add an horizontal line and vertical line in the plot to resemble the median of the x/y axis.

i know how to calculate it, but not how to display it in same plot. my code so far:

vis <- reactive({
  # Lables for axes
  xvar_name <- names(axis_vars)[axis_vars == input$xvar]
  yvar_name <- names(axis_vars)[axis_vars == input$yvar]

  xvar <- prop("x", as.symbol(input$xvar))
  yvar <- prop("y", as.symbol(input$yvar))

  gegevens %>%
    ggvis(x = xvar, y = yvar) %>%
    layer_points(size := 50, size.hover := 200,
                 fillOpacity := 0.2, fillOpacity.hover := 0.5,
                 stroke = ~bron, key := ~Project.ID) %>%
    add_tooltip(gegevens_tooltip, "hover") %>%
    add_axis("x", title = xvar_name, format='d', grid = FALSE) %>%
    add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
        add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
    scale_numeric("x", trans = "log", expand=0) %>%
    scale_numeric("y", trans = "log", expand=0) %>%
    scale_nominal("stroke", domain = c("A", "B"),
                  range = c("blue", "#aaa")) %>%
    set_options(width = 600, height = 600)
})

vis %>% bind_shiny("plot1")

to calculate the median i use:

    output$defects <- renderText ({
  d <- median(gegevens()$Total.Defects.Delivered) 
  paste("de mediaan voor totaal aantal Defects is:", d)
})

Lots of thanks for helping.

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • I think [this](http://stackoverflow.com/questions/31666210/how-can-i-add-a-horizontal-line-in-ggvis) will answer your question, either the answer or the comment. – aosmith Apr 15 '16 at 16:55
  • its does seems a good idea to use mutate and then render it on to the plot. But i just can't get it to work. The problem being that the median needs to be calculated each time an input is changed. as shown above, i know how to calculate the median even in reactive. But i don't know were to add it to the plot. as its gets calculated after the plot is made, how do i get it in the plot? might be simple, but i just can't get it to work. – Rob Dohmen Apr 18 '16 at 06:17

1 Answers1

0

Seems i misunderstood your example, but i got it working, just after i posted i couldn't. Well here is the solution:

vis <- reactive({
  # Lables for axes
  xvar_name <- names(axis_vars)[axis_vars == input$xvar]
  yvar_name <- names(axis_vars)[axis_vars == input$yvar]

  xvar <- prop("x", as.symbol(input$xvar))
  yvar <- prop("y", as.symbol(input$yvar))

  gegevens %>%
    ggvis(x = xvar, y = yvar) %>%
    layer_points(size := 50, size.hover := 200,
                 fillOpacity := 0.2, fillOpacity.hover := 0.5,
                 stroke = ~bron, key := ~Project.ID) %>%
    add_tooltip(gegevens_tooltip, "hover") %>%
    add_axis("x", title = xvar_name, format='d', grid = FALSE, properties = axis_props(labels = list(angle = 90, align = "left"))) %>%
    add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
        add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
    scale_numeric("x", trans = "log", expand=0) %>%
    scale_numeric("y", trans = "log", expand=0) %>%
    scale_nominal("stroke", domain = c("A", "B"),
                  range = c("blue", "#aaa")) %>%
    set_options(width = 600, height = 600) %>%
    layer_paths(data = gegevens, x = median(gegevens()$kolomname.i.want.the.median.from)), y = yvar ) %>%
    layer_paths(data = gegevens, x = xvar, y = median(gegevens()$kolomname.i.want.the.median.from))
})

this gives me an cross in my plot by calculating the median of x and y, even if the user changes the original input. of course i need to find out how to get "kolomname.i.want.the.median.from" to be the x-/ or y-value.

but i now know how to get the lines in, and that was the question. So thank you aosmith for the right direction.