交互式ggvis图上的叠加图例

时间:2015-10-20 05:57:51

标签: r ggvis

我想把我的传奇作为answered in this question放在情节中,但是要用于互动情节。

在第一个代码块中,图例从图中消失,但当我删除它时,它会起作用。

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

如何在交互式情节中叠加图例?

1 个答案:

答案 0 :(得分:2)

这似乎是一个悬而未决的问题。我在https://github.com/rstudio/ggvis/issues/347找到的简要解决方法:添加

%>% set_options(duration=0)

到情节的结尾:

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)

它不会重绘图例,因此不会消失。