【问题标题】:How to draw loess estimation in GGally using ggpairs?如何使用 ggpairs 在 GGally 中绘制黄土估计?
【发布时间】:2014-05-05 11:40:06
【问题描述】:

我尝试了一点 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")

谢谢!

附:与 plotmatrix 函数相比,ggpairs 要慢得多……因此,大多数时候,我只是使用 ggplot2 中的 plotmatrix。

【问题讨论】:

    标签: r ggplot2 smoothing ggally


    【解决方案1】:

    通常最好编写自己的函数供它使用。 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)
    )
    

    产生这个:

    【讨论】:

      【解决方案2】:

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

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

        ls('package:GGally')

        GGally::ggpairs

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

        seems like the args get mapped into ggpairsPlots and then -&gt; plotMatrix which then gets called

        所以显然不明确支持选择更平滑,您只能选择continuous = "smooth"。如果它的行为类似于ggplot2:geom_smooth,它会在内部自动确定要调用哪些支持的平滑器(对于 =1000 的 gam)。 您可能希望通过调试器单步执行它,以查看您的情节中发生了什么。我试图追踪消息来源,但我的眼睛呆滞了。

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

      【讨论】:

      • 谢谢!在我的代码中,数据只有 200 行,所以应该使用 loess 方法。但是,情况并非如此...... ggpairs 使用 lm。
      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2016-05-07
      • 1970-01-01
      • 2014-03-26
      • 2015-04-10
      相关资源
      最近更新 更多