Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell ggpairs to use log scales?

Tags:

r

ggplot2

Can I provide a parameter to the ggpairs function in the GGally package to use log scales for some, not all, variables?

like image 833
Karsten W. Avatar asked Aug 03 '11 23:08

Karsten W.


3 Answers

It's probably better use a linear scale and log transform variables as appropriate before supplying them to ggpairs because this avoids ambiguity in how the correlation coefficients have been computed (before or after log-transform). This can be easily achieved e.g. like this:

library(tidyverse)

log10_vars <- vars(ends_with(".Length"))             # define variables to be transformed

iris %>%                                             # use standard R example dataframe
  mutate_at(log10_vars, log10) %>%                   # log10 transform selected columns
  rename_at(log10_vars, sprintf, fmt="log10 %s") %>% # rename variables accordingly
  GGally::ggpairs(aes(color=Species))

example ggpairs plot with log transformed variables

like image 175
jan-glx Avatar answered Oct 31 '22 09:10

jan-glx


You can't provide the parameter as such (a reason is that the function creating the scatter plots is predefined without scale, see ggally_points), but you can change the scale afterward using getPlot and putPlot. For instance:

custom_scale <- ggpairs(data.frame(x=exp(rnorm(1000)), y=rnorm(1000)),
upper=list(continuous='points'), lower=list(continuous='points'))
subplot <- getPlot(custom_scale, 1, 2) # retrieve the top left chart
subplotNew <- subplot + scale_y_log10() # change the scale to log
subplotNew$type <- 'logcontinuous' # otherwise ggpairs comes back to a fixed scale
subplotNew$subType <- 'logpoints'
custom_scale <- putPlot(custom_fill, subplotNew, 1, 2)
like image 22
Jean-Robert Avatar answered Oct 31 '22 09:10

Jean-Robert


This is essentially the same answer as Jean-Robert but looks much more simple (approachable). I don't know if it is a new feature but it doesn't look like you need to use getPlot or putPlot anymore.

custom_scale[1,2]<-custom_scale[1,2] + scale_y_log10() + scale_x_log10()

Here is a function to apply it across a big matrix. Supply the number of rows in the plot and the name of the plot.

scalelog2<-function(x=2,g){  #for below diagonal
 for (i in 2:x){ 
    for (j in 1:(i-1)) { 
      g[i,(j)]<-g[i,(j)] + scale_x_continuous(trans='log2') +
scale_y_continuous(trans='log2')
                        } } 
 for (i in 1:x){  #for the bottom row 
      g[(x+1),i]<-g[(x+1),i] + scale_y_continuous(trans='log2') 
                       } 
 for (i in 1:x){ #for the diagonal
      g[i,i]<-g[i,i]+ scale_x_continuous(trans='log2')  } 
  return(g) }
like image 45
Thomas Leete Avatar answered Oct 31 '22 11:10

Thomas Leete