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)
)))
# 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)
)))
How can I overlay the legend in an interactive plot?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With