简体   繁体   English

交互式ggvis图上的叠加图例

[英]Overlay legend on interactive ggvis plot

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 我在https://github.com/rstudio/ggvis/issues/347上找到的简要解决方法:添加

%>% 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. 它不会重绘图例,因此不会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM