r - ggplot2的ggproto中 "non_missing_aes"的功能是什么?

标签 r ggplot2 aes ggproto

我正在为 ggplot2 编写扩展程序,发现里面新增了non_missing_aes ggproto 中的参数在ggplot2的官方文档中没有解释的和官方扩展指南 ggplot2 , 谁能告诉我它的功能,以及 required_aes 之间的区别?谢谢!

最佳答案

TLDRrequire_aes指定必须存在的美学映射 之前 一切尽在 geom_*()stat_*()传递给 ggproto 对象,而 non_missing_aes指定必须存在的美学映射 所述 ggproto 对象中定义的不同函数的必要处理步骤。
更长的解释
由于您正在编写扩展程序,我假设您熟悉如何将数据帧传递到 ggplot()并由每个相关层继承(或直接传入每个层),然后传入相关的 Geom/Stat ggproto 对象并沿途转换。non_missing_aes ,以及 required_aes , 作为此数据转换过程的一部分,在 Geom$handle_na 中被引用以及 Stat$compute_layer函数,默认情况下所有其他 Geoms & Stats 都从这些函数继承。
更具体地说,non_missing_aesremove_missing 中找到函数如下(为了清楚起见,我在下面添加了函数参数名称):

remove_missing(df = data, 
               na.rm = params$na.rm, 
               vars = c(self$required_aes, self$non_missing_aes), 
               name = snake_class(self))
来自 ?remove_missing ,我们可以看出这是 require_aes 中列出的所有列的位置。或 non_missing_aes被检查,并且在任何被检查的列中缺失值的行从数据框中删除。
但是为什么要使用 non_missing_aes ?为什么不在 require_aes 中指定所有这些列? ?查看一些 Geoms/Stats,它们实际上指定了 non_missing_aes 中的某些内容说明原因:
GeomBar (以下评论来自 GitHub 上的实际代码):
required_aes = c("x", "y"),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),
...
GeomRaster :
required_aes = c("x", "y"),
non_missing_aes = "fill",
default_aes = aes(fill = "grey20", alpha = NA),
...
GeomSegment :
required_aes = c("x", "y", "xend", "yend"),
non_missing_aes = c("linetype", "size", "shape"),
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
...
GeomPoint :
required_aes = c("x", "y"),
non_missing_aes = c("size", "shape", "colour"),
default_aes = aes(shape = 19, colour = "black", size = 1.5, fill = NA,
                  alpha = NA, stroke = 0.5),
...
StatYdensity (请注意,此 Stat 通常与 geom_violin 一起使用,在其 weight = 1 中指定 default_aes ):
required_aes = c("x", "y"),
non_missing_aes = "weight",
...
在每种情况下,non_missing_aes 中列出的美学映射是 不是 必须由用户在生成 ggplot 对象时指定,因此从一开始,数据框中可能不存在相应的列。
对于GeomBar,xmin/xmax/ymin/ymax 列只计算来自 GeomBar$setup_data()期间给定的数据帧.其余的,non_missing_aes映射包含在它们各自的 Geoms 中 default_aes ,所以如果用户包含类似 colour = <some variable in the data> 的内容,它们可能从一开始就存在。在 geom_*() ,否则将在稍后阶段创建列,并填充默认值。
在任何一种情况下,当数据帧被 remove_missing 评估时函数,required_aes 中的所有列或 non_missing_aes应该存在,但由于不是所有的都是由用户从一开始就输入的,我们不能在 required_aes 中指定所有这些,因为在 required_aes 中列出的任何美学映射但不存在于 geom_*()/stat_*()会触发一个错误:

Error: geom_* requires the following missing aesthetics: some_aes_or_other

关于r - ggplot2的ggproto中 "non_missing_aes"的功能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724484/

相关文章:

java - 需要 AES 中错误的 IV 长度的解决方案

java - 生成给定长度的 AES key ,其中包含 a-Z0-9

r - 使用多变量 xreg 进行 auto.arima 预测 - 意外结果

r - 将 GLM 的公式设置为 R 中列的总和

r - 为一个ggplot2图创建两个图例并修改它们

r - 有没有办法绘制障碍模型结果 pscl 包或绘制计数 - r 中障碍模型的零截断 negbin 部分?

r - 如何使用函数通过ggplot2转换轴值?

r - geom_tile 图的可变大小

arduino - 如何使用AES加密大数据

mysql - RMySQL 编译失败 : R and R CMD