Search code examples
rplotdplyrggvisshiny

Plotting : Can't get log axis to work (with Shiny + GGvis + Dplyr)


I have some code written for a reactive shiny app, which has variable axes. I get them to display just as I want, except for one thing. I need both axes to be on a log-scale instead of linear. I found solutions as trans = "log" but that causes my graph to show up empty. Any solutions?

The code i have for plotting (if i change log to linear, its works):

vis <- reactive({
  # Labels 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) %>%
    add_axis("y", title = yvar_name) %>%
    add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
    scale_numeric("x", trans = "log") %>%
    scale_numeric("y", trans = "log") %>%
    scale_nominal("stroke", domain = c("A", "B"),
                  range = c("blue", "#aaa")) %>%
    set_options(width = 600, height = 600)
})

vis %>% bind_shiny("plot1")

Solution

  • By adding expand to the code its becomes a log scale. i also admit format="d", grid=FALSE to add_axis so it looked a bit better. still not satisfied with the values on the axis, but that's an other question.

    code i replaced:

    add_axis("x", title = xvar_name, format='d', grid = FALSE) %>%
    
    scale_numeric("x", trans = "log", expand=0) %>%