简体   繁体   English

ggvis:混乱的父层属性继承

[英]ggvis: Confusing inheritance of properties from parent layer

I have data frame like: 我有像这样的数据框:

   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

which contains the positions for rectangles and the labels. 其中包含矩形和标签的位置。 But when I try to add the text layer, I get a error message that x2 and y2 are unknown properties: 但是,当我尝试添加文本层时,出现错误消息,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?

How can I tell to ggvis to remove x2 and y2 for the text layer? 如何告诉ggvis删除文本层的x2和y2?

I already tried the following, because the description of 'inherit' sounds promising: 我已经尝试了以下方法,因为对“继承”的描述听起来很有希望:

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)

but this fails with following error: 但这失败,并显示以下错误:

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

One workaround is to specify all properties in all layers again, but I hope there is a better solution :) 一种解决方法是再次在所有层中指定所有属性,但我希望有更好的解决方案:)

Your guess is correct: you should use inherit = FALSE , but there's no need to wrap it into props() call. 您的猜测是正确的:您应使用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)

在此处输入图片说明

From a stylistic point of view, you should declare only "universal" mappings within ggvis , while keeping layer-specific ones within layer_* . 从样式角度来看,应仅在ggvis声明“通用”映射,而在layer_*保留特定于层的映射。 In that case, the unwanted inheritance won't be a problem: 在这种情况下,不需要的继承将不会成为问题:

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)

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

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