Ggplot2 - summary summary statistics / levels

I took the ggplot2 book, but I'm struggling to figure out how the data is saved through the layers.

For example, let's take a dataset and calculate the average for each X:

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

How do I access summary statistics at the next level? For example, let's say I want to build a smooth line over a dataset. This seems to work:

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

But let's say I want to further ignore the specific value of X when creating the string? How to access a generic dataset to express a specific X exception?

More generally, how is data tied to a stream through layers? Have I always limited myself to the latest statistics? Can I reference the source dataset?

+4
source share
1 answer

Here is an attempt to answer your question

  • The aesthetics defined in the ggplot call are used as default values ​​in all subsequent layers, unless explicitly defined. This is why geom_smooth works
  • You can specify data frame and aesthetics for each layer separately. For example, if you want to exclude some x values ​​when building geom_smooth , you can specify subset = .(x != xvalues) inside the geom_smooth call

I can provide more detailed examples if you have specific questions.

Hope this helps

+4
source

All Articles