如何使用ggpairs在GGally中绘制黄土估计?

时间:2014-03-26 19:54:11

标签: r ggplot2 smoothing ggally

我试了一下GGally包。特别是ggpairs功能。但是,当绘制平滑时,我无法弄清楚如何使用黄土而不是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")

谢谢!

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

2 个答案:

答案 0 :(得分:1)

文档没有说明,所以使用源代码,Luke

  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,它会在内部自动确定要调用哪些支持的平滑器(黄土为&lt; 1000 datapoints,gam为&gt; = 1000)。 您可能希望通过调试器逐步查看绘图中发生的情况。我试图跟随源头,但我的眼睛茫然。

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

    #' 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');
    

答案 1 :(得分:0)

通常最好编写自己的函数以供使用。 Adapted from this answer到类似的问题。

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

产生这个:

enter image description here