1

我尝试了一点GGally包。尤其是 ggpairs 函数。但是,当情节平滑时,我无法弄清楚如何使用 loess 而不是 lm。有任何想法吗?这是我的代码:

require(GGally)
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
ggpairs(diamonds.samp[,c(1,5)], 
        lower = list(continuous = "smooth"),
        params = c(method = "loess"),
        axisLabels = "show")

谢谢!

PS 与 plotmatrix 函数相比,ggpairs 要慢得多......结果,大多数时候,我只是使用 ggplot2 中的 plotmatrix。

4

2 回答 2

1

那么文档没有说,所以使用源,卢克

  1. 您可以通过以下方式深入挖掘源代码:

    ls('package:GGally')

    GGally::ggpairs

    ... and browse every function it references ...

    seems like the args get mapped into ggpairsPlots and then -> plotMatrix which then gets called

    所以显然没有明确支持选择更平滑,你只能选择continuous = "smooth". 如果它在内部表现得像ggplot2:geom_smooth它一样自动计算出要调用哪个支持的平滑器(<1000 数据点的黄土,> = 1000 的 gam)。您可能希望通过调试器单步执行它,以查看您的情节中发生了什么。我试图追踪消息来源,但我的眼睛呆滞了。

或 2. 浏览https://github.com/ggobi/ggally/blob/master/R/ggpairs.r [4/14/2013]

#' upper and lower are lists that may contain the variables 'continuous',
#' 'combo' and 'discrete'. Each element of the list is a string implementing
#' the following options: continuous = exactly one of ('points', 'smooth',
#' 'density', 'cor', 'blank') , ...
#'
#' diag is a list that may only contain the variables 'continuous' and 'discrete'.
#' Each element of the diag list is a string implmenting the following options:
#' continuous = exactly one of ('density', 'bar', 'blank');
于 2014-04-04T00:00:24.297 回答
1

通常最好编写自己的函数供它使用。改编自this answer to similar question。

library(GGally)
diamonds_sample = diamonds[sample(1:dim(diamonds)[1],200),]

# Function to return points and geom_smooth
# allow for the method to be changed
custom_function = function(data, mapping, method = "loess", ...){
      p = ggplot(data = data, mapping = mapping) + 
      geom_point() + 
      geom_smooth(method=method, ...)

      p
    }

# test it  
ggpairs(diamonds_sample,
    lower = list(continuous = custom_function)
)

产生这个:

在此处输入图像描述

于 2020-10-29T10:29:38.340 回答