Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay legend on interactive ggvis plot

Tags:

r

ggvis

I would like to put my legend inside the plot as answered in this question, but for an interactive plot.

In the first code chunk the legend disappears off the plot, but when I remove the interaction it works.

library(ggvis)
library(dplyr)

# With drop down list it doesn't work
mtcars %>% 
  ggvis(x = ~wt, y = input_select(c("Miles per gallon" = "mpg", "Horse power" = "hp", "Displacement"="disp", "Carbohydrates" = "carb"), map = as.name, selected = "mpg", label = "Variables"), fill=~cyl) %>% 
  layer_points() %>% 
  add_relative_scales() %>%
  add_legend("fill", title = "Cylinders",
             properties = legend_props(
               legend = list(
                 x = scaled_value("x_rel", 0.8),
                 y = scaled_value("y_rel", 1)
               )))

enter image description here

# Remove interaction and it works
mtcars %>% 
  ggvis(x = ~wt, y = ~mpg, fill = ~cyl) %>% 
  layer_points() %>% 
  add_relative_scales() %>%
  add_legend("fill", title = "Cylinders",
             properties = legend_props(
               legend = list(
                 x = scaled_value("x_rel", 0.8),
                 y = scaled_value("y_rel", 1)
               )))

enter image description here

How can I overlay the legend in an interactive plot?

like image 456
Tom Avatar asked Oct 20 '15 05:10

Tom


1 Answers

It seems this is an open issue. Brief workaround I've found on https://github.com/rstudio/ggvis/issues/347 : add

%>% set_options(duration=0)

to the end of the plot:

mtcars %>% 
  ggvis(x = ~wt, y = ~mpg, fill = ~cyl) %>% 
  layer_points() %>% 
  add_relative_scales() %>%
  add_legend("fill", title = "Cylinders",
         properties = legend_props(
           legend = list(
             x = scaled_value("x_rel", 0.8),
             y = scaled_value("y_rel", 1)
           ))) %>% set_options(duration=0)

It doesn't redraw the legend and that therefore doesn't disappear.

like image 74
JaKu Avatar answered Oct 21 '22 01:10

JaKu