gpt4 book ai didi

r - GGally:从图中提取数据

转载 作者:行者123 更新时间:2023-12-05 01:52:00 25 4
gpt4 key购买 nike

我想制作一个数据框,其中包含用于从上面的 GGally 生成 ggplot 的汇总数据(变量和相关值)。

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')"

但不幸的是,ggplot_build 不起作用。请问有什么帮助吗?

最佳答案

您可以将数据作为数据框列表获取,通过执行以下操作从每个子图中获取一个数据框:

result <- lapply(p$plots, function(x) ggplot_build(x$fn(p$data, x$mapping))$data)

到目前为止,第一个图(密度曲线)的数据框开始于:

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

第一个相关标签是:

 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

reprex package 创建于 2022-04-14 (v2.0.1)

关于r - GGally:从图中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71876162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com