【问题标题】:Overlay legend on interactive ggvis plot在交互式 ggvis 图上叠加图例
【发布时间】:2016-01-18 15:57:19
【问题描述】:

我想将我的图例以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)
               )))

# 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)
               )))

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

【问题讨论】:

  • 使用您的第一段代码,当我选择Horse power 时,图例出现了。但是当我选择Miles per gallon 时它消失了。由于 y 轴的值会根据用户的选择而变化,我想知道这个 y-lim 变化是否与此有关。我以某种方式认为图例位置设置为最大 y 值。 Horse power 有超过 300 个,而 Miles per gallon 只有 34 个。所以当一个人选择后者时,传说就消失了……这只是我的猜测。
  • @jazzurro 有趣!谢谢你的猜测。虽然,它似乎是任意的其他选择。我在下拉菜单中添加了另外两个选项,如果您选择Horse power,然后选择Displacement,然后选择Miles per gallon,则会出现。
  • 这对我来说似乎很麻烦。我想知道这个问题是否在github上提出。可能值得报告这个案例。

标签: r ggvis


【解决方案1】:

这似乎是一个悬而未决的问题。我在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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多