Jellz
Jellz

Reputation: 455

How to return the results from a summary function?

I made a ggplot, I would like to use the results from a function.

How can I return the result of stat_summary_2d?

ggplot(diamonds, aes(carat, depth, z = price))+
  stat_summary_2d(fun =function(x)mean(x))

Upvotes: 1

Views: 160

Answers (2)

Jellz
Jellz

Reputation: 455

Find a solution here:

p<- ggplot(diamonds, aes(carat, depth, z = price))+
       stat_summary_2d(fun =function(x)mean(x))

library('Hmisc')
ggplot_build(p)$data[[1]]

fill   xbin ybin value          x         y  PANEL group   xmin       xmax 
#1F4364 6   1   4206.0000   1.0421667   42.6    1   -1  0.9620000   1.1223333   
#1F4262 6   2   4032.0000   1.0421667   43.8    1   -1  0.9620000   1.1223333   
#142E47 1   8   945.0000    0.2405000   51.0    1   -1  0.1603333   0.3206667   
#28557B 8   8   6727.0000   1.3628333   51.0    1   -1  1.2826667   1.4430000   
#152F49 2   9   1166.0000   0.4008333   52.2    1   -1  0.3206667   0.4810000   
#15304A 3   9   1293.0000   0.5611667   52.2    1   -1  0.4810000   0.6413333   
#17344F 4   9   1895.0000   0.7215000   52.2    1   -1  0.6413333   0.8016667   
#152E47 2   10  1011.5000   0.4008333   53.4    1   -1  0.3206667   0.4810000   
#183651 4   10  2164.0000   0.7215000   53.4    1   -1  0.6413333   0.8016667   
#1B3A57 5   10  2815.0000   0.8818333   53.4    1   -1  0.8016667   0.9620000   

Upvotes: 0

Pierre Gramme
Pierre Gramme

Reputation: 1254

If you are asking how to re-use ggplot2's implementation of the statistics summary function, it is currently not easily feasible with the public API. You can have a look at ggplot2's internals and copy-paste the parts you need. But this should be seen as a non-robust hack which may break after any package upgrade.

The core function of bin2d is accessible in ggplot2::StatBin2d$compute_group. But to call it, you will need some of the plot's components which are not easy to construct by hand.

In another answer, I had listed all the steps done when building a graph in a typical case. Function ggplot2:::ggplot_build.ggplot will show you the details. The relevant line calling the statistics computation is

data <- by_layer(function(l, d) l$compute_statistic(d, layout))

You'll need to execute all previous steps before starting it. On your example, it would finally give:

# A tibble: 238 x 9
    xbin  ybin value     x width     y height PANEL group
   <int> <int> <dbl> <dbl> <dbl> <dbl>  <dbl> <fct> <int>
 1     6     1 4206  1.04  0.160  42.6   1.20 1        -1
 2     6     2 4032  1.04  0.160  43.8   1.20 1        -1
 3     1     8  945  0.240 0.160  51.0   1.2  1        -1
 4     8     8 6727  1.36  0.160  51.0   1.2  1        -1
 5     2     9 1166  0.401 0.160  52.2   1.20 1        -1
 6     3     9 1293  0.561 0.160  52.2   1.20 1        -1
 7     4     9 1895  0.722 0.160  52.2   1.20 1        -1
 8     2    10 1012. 0.401 0.160  53.4   1.2  1        -1
 9     4    10 2164  0.722 0.160  53.4   1.2  1        -1
10     5    10 2815  0.882 0.160  53.4   1.2  1        -1
# ... with 228 more rows

Trick: internal functions of a package can be accessed with triple colon pkg:::fun.

Upvotes: 2

Related Questions