简体   繁体   English

ggplot2 - 引用摘要统计信息/图层

[英]ggplot2 - referecing summary statistics / layers

I've picked-up the ggplot2 book but I'm struggling to understand how data persists through layers. 我已经拿到了ggplot2的书,但我很难理解数据如何通过图层持续存在。

For example, lets take a dataset and calculate the mean of each X: 例如,让我们获取数据集并计算每个X的平均值:

thePlot = ggplot( myDF , aes_string( x = "IndepentVar" , y = "DependentVar" ) )
thePlot = thePlot + stat_summary( fun.y = mean , geom = "point" )

How do I "access" the summary statistics in the next layer? 如何在下一层“访问”摘要统计信息? For example, lets say I want to plot a smooth line over the dataset. 例如,假设我想在数据集上绘制一条平滑线。 This seems to work: 这似乎有效:

thePlot = thePlot + stat_smooth( aes( group = 1 ) , method = "lm" , geom = "smooth" , se = FALSE )

But lets say I want to further ignore a particular X value when generating the line? 但是,我想在生成线时想进一步忽略特定的X值? How do I reference the summarized dataset to express excluding a particular X? 如何引用汇总数据集以表示排除特定X?

More generally, how is data referenced as it flows through layers? 更一般地说,当数据流过层时如何引用数据? Am I always limited to the last statistics? 我总是限于最后的统计数据吗? Can I reference the original dataset? 我可以参考原始数据集吗?

Here is an attempt at answering your question 这是尝试回答您的问题

  1. The aesthetics defined in the ggplot call, get used as defaults in all subsequent layers if they are not explicitly defined. 如果未明确定义ggplot调用中定义的美学,则将其用作所有后续层中的默认值。 That is the reason geom_smooth works 这就是geom_smooth工作的原因
  2. You can specify the data frame and aesthetics for each layer separately. 您可以分别为每个图层指定data frameaesthetics For example if you want to exclude some values of x while plotting geom_smooth , you can specify subset = .(x != xvalues) inside the geom_smooth call 例如,如果要在绘制geom_smooth排除某些x值,可以在geom_smooth调用中指定subset = .(x != xvalues)

I can provide more detailed examples, if you have specific questions. 如果您有具体问题,我可以提供更详细的示例。

Hope this helps 希望这可以帮助

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

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