简体   繁体   English

R, ggplot2: How can I visualize the inheritance or flow of data, attributes, and other components of a graphical object in ggplot2?

[英]R, ggplot2: How can I visualize the inheritance or flow of data, attributes, and other components of a graphical object in ggplot2?

I am often unsure of exactly which elements of data, attributes, and other components of a graphical object in ggplot2 are inherited by which other elements, and where the defaults that flow down to, eg, geoms, originate.我经常不确定 ggplot2 中图形ggplot2的数据、属性和其他组件的确切元素是由哪些其他元素继承的,以及向下流向的默认值(例如,geoms)的来源。 In particular cases these questions can generally be answered by close reading of Hadley's ggplot2 book.在特定情况下,这些问题通常可以通过仔细阅读 Hadley 的ggplot2书来回答。 But I would find it useful to have some sort of visualization of the overall flow of inheritance in ggplot2 , and I wonder if anyone has seen, or created, or knows how to create, such a thing.但我会发现在ggplot2中对 inheritance 的整体流程进行某种可视化是很有用的,我想知道是否有人看过、创建或知道如何创建这样的东西。 I the same vein, a compact list of default values which arise in one level of specification (like the aes or a theme) and are inherited by another level (like a geom or scale) would be very useful to me, and I suspect to many people learning how to use ggplot2.同样,在一个规范级别(如 aes 或主题)中出现并由另一个级别(如几何或比例)继承的默认值的紧凑列表对我非常有用,我怀疑许多人学习如何使用 ggplot2。

I would accept any of the following as an answer:我会接受以下任何一项作为答案:

  1. An inheritance visualization (perhaps as a network?) or a pointer to same. inheritance 可视化(可能作为网络?)或指向相同的指针。
  2. Code to construct an inheritance visualization, or a pointer to same.用于构造 inheritance 可视化的代码,或指向相同的指针。
  3. An alternative, non-visual approach that makes it easy, or at least easier, to understand and remember such inheritance and answer specific questions about it.另一种非可视化方法使理解和记住此类 inheritance 并回答有关它的具体问题变得容易或至少更容易。
  4. A list specifically of argument defaults showing where they arise and which subsidiary functions inherit them, or code to produce such a list.具体的参数默认列表,显示它们出现的位置以及哪些辅助函数继承它们,或生成此类列表的代码。

This question seems to be about multiple levels of the ggplot package at once, but I'll try my best to give some information.这个问题似乎是一次关于 ggplot package 的多个级别,但我会尽力提供一些信息。 It's almost impossible to describe the entire inheritance system of ggplot in one stack overflow answer, but pointing at the right functions might help get you started.在一个堆栈溢出答案中描述 ggplot 的整个 inheritance 系统几乎是不可能的,但是指出正确的函数可能会帮助您入门。

At the top level, both data and aesthetic mappings are inherited from the main ggplot call.在顶层,数据和美学映射都继承自主 ggplot 调用。 In code below, geom_point() inherits the mapping and data:在下面的代码中, geom_point()继承了映射和数据:

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point()

Unless you explicitly provide an alternative mapping and set the inheritance to false:除非您明确提供替代映射并将 inheritance 设置为 false:

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(Petal.Width, Petal.Length), inherit.aes = FALSE)

Next, at the level of individual layers, certain defaults are inherited from either the stats, geoms or positions.接下来,在各个层的级别上,某些默认值是从统计数据、几何图形或位置继承而来的。 Consider the following plot:考虑以下 plot:

df <- reshape2::melt(volcano)

ggplot(df, aes(Var1, Var2)) +
  geom_raster()

The raster will be a dark grey colour, because we haven't specified a fill mapping.栅格将是深灰色,因为我们没有指定填充映射。 You can get a sense what defaults a geom / stat has by looking at their ggproto objects:您可以通过查看它们的ggproto对象来了解 geom / stat 的默认值:

> GeomRaster$default_aes
Aesthetic mapping: 
* `fill`  -> "grey20"
* `alpha` -> NA

> StatDensity$default_aes
Aesthetic mapping: 
* `y`    -> `stat(density)`
* `fill` -> NA

Another key ingredient for understanding how layers are given their parameters, is looking at the layer() code.理解层如何被赋予参数的另一个关键因素是查看layer()代码。 Specifically, this bit here (abbreviated for clarity):具体来说,这里的这一点(为清楚起见而缩写):

function (geom = NULL, stat = NULL, data = NULL, mapping = NULL, 
  position = NULL, params = list(), inherit.aes = TRUE, check.aes = TRUE, 
  check.param = TRUE, show.legend = NA, key_glyph = NULL, 
  layer_class = Layer) 
{
...
  aes_params <- params[intersect(names(params), geom$aesthetics())]
  geom_params <- params[intersect(names(params), geom$parameters(TRUE))]
  stat_params <- params[intersect(names(params), stat$parameters(TRUE))]
...
  ggproto("LayerInstance", layer_class, geom = geom, geom_params = geom_params, 
    stat = stat, stat_params = stat_params, data = data, 
    mapping = mapping, aes_params = aes_params, position = position, 
    inherit.aes = inherit.aes, show.legend = show.legend)
}

Wherein you can see that whatever parameters you give, they are checked against valid parameters of the stat/geom/position and distributed to the appropriate part of the layer.您可以在其中看到,无论您提供什么参数,它们都会根据 stat/geom/position 的有效参数进行检查,并分发到图层的适当部分。 As you can see from the last call, a Layer ggproto object is created.从上次调用中可以看到,创建了一个 Layer ggproto object。 The parent of this class is not exported but you can still inspect the functions inside that object.此 class 的父级未导出,但您仍然可以检查 object 内部的函数。 For example, if you are curious about how aesthetics are evaluated, you can type:例如,如果您对如何评估美学感到好奇,您可以输入:

ggplot2:::Layer$compute_aesthetics

Wherein you can see that some defaults of the scales are incorporated here as well.其中您可以看到,这里也包含了一些默认的比例。 Of course, it doesn't make much sense what these layers do if you don't understand the order of operations in which these layer functions are called.当然,如果你不了解这些层函数被调用的操作顺序,那么这些层的作用并没有多大意义。 For that we can have a look at the plot builder (also abbreviated for clarity):为此,我们可以查看 plot 构建器(为清楚起见也缩写):

> ggplot2:::ggplot_build.ggplot
function (plot) 
{
...
    data <- by_layer(function(l, d) l$setup_layer(d, plot))
...
    data <- by_layer(function(l, d) l$compute_aesthetics(d, plot))
    data <- lapply(data, scales_transform_df, scales = scales)
...
    data <- layout$map_position(data)
    data <- by_layer(function(l, d) l$compute_statistic(d, layout))
    data <- by_layer(function(l, d) l$map_statistic(d, plot))
    scales_add_missing(plot, c("x", "y"), plot$plot_env)
    data <- by_layer(function(l, d) l$compute_geom_1(d))
    data <- by_layer(function(l, d) l$compute_position(d, layout))
...
    data <- by_layer(function(l, d) l$compute_geom_2(d))
    data <- by_layer(function(l, d) l$finish_statistics(d))
...
    structure(list(data = data, layout = layout, plot = plot), 
        class = "ggplot_built")
}

From this, you can see that layers are setup first, then aesthetics are computed, then scale transformations are applied, then statistics are computed, then a part of the geom is computed, then the position, and finally the reset of the geom.从这里可以看出,首先设置图层,然后计算美学,然后应用比例变换,然后计算统计信息,然后计算部分几何图形,然后是 position,最后是几何图形的重置。

What this means is that the statistical transformations you put in are going to be affected by scale transformations, but not coord transformations (which is elsewhere later).这意味着您输入的统计转换将受到比例转换的影响,而不是坐标转换(稍后在其他地方)。

If you go through the code, you'll find that almost nothing theme-related is evaluated up untill this point (except for some theme evaluation with the facets).如果您通过代码 go ,您会发现到目前为止几乎没有任何与主题相关的评估(除了一些带有方面的主题评估)。 As you can see, the building function returns an object of the class ggplot_build , which is still not graphical output.如您所见,建筑 function 返回 object 的 class ggplot_build ,这仍然不是图形 Z7148CE6221F69393。 The interpretation of theme elements and actual interpretation of the geoms towards grid graphics happens in the following function:主题元素的解释和geoms对网格图形的实际解释发生在以下function中:

ggplot2:::ggplot_gtable.ggplot_built

After this function, you'll have a gtable object that can be interpreted by grid::grid.draw() which will output to your graphics device.在此 function 之后,您将拥有一个 gtable object,它可以由grid::grid.draw()解释,它将 output 到您的图形设备。

Unfortunately, I'm not very well-versed in the inheritance of theme elements, but as Jon Spring pointed out in the comments, a good place to start is the documentation.不幸的是,我对主题元素的 inheritance 不是很精通,但正如 Jon Spring 在评论中指出的那样,文档是一个很好的起点。 Hopefully, I've pointed out functions where to look for inheritance patterns in ggplot.希望我已经指出了在 ggplot 中查找 inheritance 模式的功能。

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

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