【问题标题】:ggvis: Confusing inheritance of properties from parent layerggvis:混淆从父层继承属性
【发布时间】:2015-09-09 08:26:07
【问题描述】:

我有这样的数据框:

   x1  x2 y1 y2 labels       colors
1 1.0 1.1  1  2      A   blueviolet
2 1.3 1.4  1  2      A       azure1
3 1.0 1.1  3  4      B navajowhite3
4 1.3 1.4  3  4      B       grey46

其中包含矩形和标签的位置。但是当我尝试添加文本层时,我收到一条错误消息,指出 x2 和 y2 是未知属性:

ggvis(data = df2, x = ~x1, y = ~y1, x2 = ~x2, y2 = ~y2) %>% 
   layer_rects(fill = ~colors) %>% 
   layer_text(x = ~ x1 - 1, y = ~y1 + 0.4, text := ~labels)

Error: Unknown properties: x2, y2.
Did you mean: x, y?

如何告诉ggvis 删除文本层的 x2 和 y2?

我已经尝试了以下方法,因为“继承”的描述听起来很有希望:

ggvis(data = df2, x = ~x1, y = ~y1, x2 = ~x2, y2 = ~y2) %>% 
   layer_rects(fill = ~colors) %>% 
   layer_text(props(x = ~ x1 - 1, y = ~y1 + 0.4, inherit = FALSE), 
              text := ~labels)

但这失败并出现以下错误:

Error in new_prop.default(x, property, scale, offset, mult, env, event,  : 
  Unknown input to prop: list(property = "x", value = x1 - 1, scale = "x", offset = NULL, mult = NULL, event = "update", env = <environment>)list(property = "y", value = y1 + 0.4, scale = "y", offset = NULL, mult = NULL, event = "update", env = <environment>)

一种解决方法是再次指定所有层中的所有属性,但我希望有更好的解决方案:)

【问题讨论】:

    标签: r shiny ggvis


    【解决方案1】:

    您的猜测是正确的:您应该使用inherit = FALSE,但无需将其包装到props() 调用中。

    ggvis(data = df2, x = ~x1, y = ~y1, x2 = ~x2, y2 = ~y2) %>% 
      layer_rects(fill = ~colors) %>% 
      layer_text(text := ~labels, inherit = F, x = ~x1 + 0.05, y = ~y1 + 0.5, fontSize := 40)
    

    从风格的角度来看,您应该只在ggvis 中声明“通用”映射,同时在layer_* 中保留特定层的映射。在这种情况下,不需要的继承不会成为问题:

    ggvis(data = df2) %>% 
      layer_rects(fill = ~colors, x = ~x1, y = ~y1, x2 = ~x2, y2 = ~y2) %>% 
      layer_text(text := ~labels, x = ~x1 + 0.05, y = ~y1 + 0.5, fontSize := 40)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多