I'd like to make a data frame that contains the summarized data (Variables and correlation values) used to generate the ggplot
from GGally
above.
library(ggplot2)
library(GGally)
data(flea)
p <- ggpairs(flea, columns = 2:4)
p.2.df <- ggplot_build(p)
Error in UseMethod("ggplot_build") :
no applicable method for 'ggplot_build' applied to an object of class "c('gg', 'ggmatrix')"
But unfortunately, the ggplot_build
doesn't work.
Please any help with it?
You can get the data as a list of data frames, one from each sub-plot by doing:
result <- lapply(p$plots, function(x) ggplot_build(x$fn(p$data, x$mapping))$data)
so far example, the data frame for the first plot (a density curve) starts:
result[[1]]
#> [[1]]
#> y x density scaled ndensity count n
#> 1 0.0045919002 122.0000 0.0045919002 0.31011213 0.31011213 0.33980062 74
#> 2 0.0046762616 122.2348 0.0046762616 0.31580944 0.31580944 0.34604336 74
#> 3 0.0047606795 122.4697 0.0047606795 0.32151057 0.32151057 0.35229028 74
#> 4 0.0048451404 122.7045 0.0048451404 0.32721460 0.32721460 0.35854039 74
#> 5 0.0049295633 122.9393 0.0049295633 0.33291607 0.33291607 0.36478768 74
#> 6 0.0050139632 123.1742 0.0050139632 0.33861598 0.33861598 0.37103327 74
#> 7 0.0050981826 123.4090 0.0050981826 0.34430371 0.34430371 0.37726551 74
#> 8 0.0051822881 123.6438 0.0051822881 0.34998373 0.34998373 0.38348932 74
#> 9 0.0052661612 123.8787 0.0052661612 0.35564807 0.35564807 0.38969593 74
#> 10 0.0053497430 124.1135 0.0053497430 0.36129274 0.36129274 0.39588098 74
#> 11 0.0054331166 124.3483 0.0054331166 0.36692333 0.36692333 0.40205062 74
#> 12 0.0055159428 124.5832 0.0055159428 0.37251697 0.37251697 0.40817977 74
#> 13 0.0055985890 124.8180 0.0055985890 0.37809845 0.37809845 0.41429558 74
#> 14 0.0056804992 125.0528 0.0056804992 0.38363022 0.38363022 0.42035694 74
#> 15 0.0057620771 125.2877 0.0057620771 0.38913955 0.38913955 0.42639371 74
#> 16 0.0058430226 125.5225 0.0058430226 0.39460618 0.39460618 0.43238367 74
#> 17 0.0059232982 125.7573 0.0059232982 0.40002755 0.40002755 0.43832407 74
#> 18 0.0060031239 125.9922 0.0060031239 0.40541855 0.40541855 0.44423117 74
#> 19 0.0060818643 126.2270 0.0060818643 0.41073625 0.41073625 0.45005796 74
#> 20 0.0061602651 126.4618 0.0061602651 0.41603102 0.41603102 0.45585962 74
...
etc
and the first correlation label is :
result[[3]]
[[1]]
x y PANEL group colour size angle hjust vjust alpha family fontface lineheight label
1 50.5 182 1 -1 #717171 3.88 0 0.5 0.5 NA 1 1.2 Corr:\n-0.096
Created on 2022-04-14 by the reprex package (v2.0.1)