How to draw loess estimation in GGally using ggpairs?
Asked Answered
C

2

2

I tried GGally package a little bit. Especially the ggpairs function. However, I cannot figure out how to use loess instead of lm when plot smooth. Any ideas? Here is my code:

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

Thanks!

P.S. compare with the plotmatrix function, ggpairs is much much slower... As a result, most of the time, I just use plotmatrix from ggplot2.

Colicroot answered 26/3, 2014 at 19:54 Comment(0)
M
2

Often it is best to write your own function for it to use. Adapted from 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)
)

Produces this:

enter image description here

Misprize answered 29/10, 2020 at 10:29 Comment(0)
E
1

Well the documentation doesn't say, so use the source, Luke

  1. You can dig deeper into the source with:

    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

    So apparently selecting smoother is not explicitly supported, you can only select continuous = "smooth". If it behaves like ggplot2:geom_smooth it internally automatically figures out which of the supported smoothers to call (loess for <1000 datapoints, gam for >=1000). You might like to step it through the debugger to see what's happening inside your plot. I tried to follow the source but my eyes glaze over.

or 2. Browse on 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');
Extrados answered 4/4, 2014 at 0:0 Comment(1)
Thanks! In my code, the data have only 200 rows, so it should be using the loess method. But, this is not the case...the ggpairs uses the lm.Colicroot

© 2022 - 2024 — McMap. All rights reserved.