Search code examples
rggplot2visualizationggvis

Align multiple graphs using ggvis (layer_points over layer_boxplots)


I'm trying to layer points over a boxplot. Both the points and the boxplot come from the same data source: db_gems_spend. The only difference is how they are filtered (range of dates for boxplot and a single day for points). The end goal is to add interactivity to the graph so that I will be able to select a date and immediately see how the day compares to other days by seeing where the point lands on a particular box plot.

The problem is that the points do not currently align with the box plots.

You can see it here: enter image description here

This is the code:

db_gems_spend %>%
  filter(dayofweek == "Fri") %>% # add interactivity (automate dayofweek selection)
  filter(date >= "2015-08-01") %>% # add interactivity
  ggvis(~action_type, ~count) %>%
  layer_boxplots() %>%
  add_axis("x", title = "action_type", title_offset = 50, 
           properties = axis_props(labels = list(angle = 20, align = "left", fontSize = 10))) %>%
  add_axis("y", title = "count", title_offset = 60) %>%
  add_data(db_gems_spend) %>%
  filter(date == "2015-11-04") %>% # add interactivity
  layer_points(x = ~action_type, y = ~count, fill :=  "red")

How can I get these points to align?


Solution

  • db_gems_spend %>%
      ggvis(~action_type, ~(count/total_spend)) %>%
      layer_boxplots() %>%
      add_data(db_gems_spend) %>%
      layer_points(x = ~action_type, y = ~count, fill := "red", 
        prop("x", ~action_type, scale = "xcenter"))
    

    Thanks aosmith, the solution on github was what I was looking for. It turns out ggvis will align layer_points to the left of layer_boxplots if the values are categorical and not numerical unless you specify the last line of code from above.