ggvis:混淆父层属性的继承

ggvis: Confusing inheritance of properties from parent layer

我有这样的数据框:

   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?

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

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

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

您的猜测是正确的:您应该使用 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 中声明 "universal" 映射,同时在 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)