1

I have 10 variables and I want to plot them against each other in R. If I had fewer, I could just do

pairs(data)

But unfortunately, when I do this I get really tiny plots. I have looked at this question.

Change plot size of pairs plot in R

I don't think the OP is seeking the same thing. If I understand correctly, he wants to enlarge the plot so that, when he zooms in on it in the PDF the images are plotted with more room.

However, I need to write a report and I would like to do away with the 10x10 grid entirely (unless someone has a better idea). In pairwise comparisons grids, typically there is a giant grid, where each cell in the grid is a plot. But obviously for large number of variables, this is hard to view. For 10x10 this is too small. I would like a plotting method that allows me to automatically generate all the pairwise comparisons but then prints them out in a more reasonable format. So for 10 variables, perhaps a 25 x 4 grid so the plots can be easily viewed on multiple pages.

Is there an easy way to do this in ggplot2? If not, what alternatives exist?

Here's an example:

library(Matrix)
library(mvtnorm)
set.seed(42)
n <- 1000
sig <- matrix(rexp(100, rate=.1), ncol=10)
sig <- matrix(forceSymmetric(sig), ncol=10)
X <- rmvnorm(n, mean=rep(0,10), sigma=sig, method="svd")
pairs(X)

which yields enter image description here

Stan Shunpike
  • 2,165
  • 3
  • 20
  • 32
  • 2
    Can you make this a [mcve]? There's no data, substantial code, or output that you're trying to change – camille Dec 09 '19 at 05:24
  • Do you really need all 100 plots? 10 of them are a variable against itself which is not informative and 45 are reflections (switching x and y axis for the same variables). All of the information is contained in 45 plots (e.g. 9 x 5). – dcarlson Dec 09 '19 at 17:20
  • @camille i provided a minimial reproducible example – Stan Shunpike Dec 09 '19 at 17:46
  • @dcarlson No, I don't need the reflections or the variable against itself. If there is a way to just do it without those, that would be sufficient. – Stan Shunpike Dec 09 '19 at 17:47

2 Answers2

1

Suppose your dataset is df. You can do what you want in three steps:

library(tidyverse)

# create all combinations of different variable names
nms <- list(x = names(df), 
            y = names(df)) %>% 
  cross_df() %>% 
  filter(x != y)

# create list of all the scatter plots
plts <- map2(.x = nms$x, 
             .y = nms$y,
          ~{ggplot(data = df, 
                   aes_string(x = .x, 
                              y = .y)) +
              geom_point()}
          )

# create multi-page pdf with the figures
ggsave("longplot.pdf", 
       gridExtra::marrangeGrob(grobs = plts, 
                               nrow = 5, 
                               ncol = 2),
       device = "pdf", 
       width = 210, 
       height = 297, 
       units = "mm")
Vlad
  • 912
  • 9
  • 9
0

Here's an approach with base graphics. First create a list of the plots:

plts <- expand.grid(1:10, 1:10)
plts <- as.matrix(plts[plts$Var2 < plts$Var1, ])

Set up proportions for a 9 x 5 set of 45 plots. Remove grid lines/labels since you just want to see the overall pattern:

dev.new(width=8, height=12)
oldp <- par(mfcol=c(9, 5), mar=c(1, 1, 1, 1))
for (i in seq_len(dim(plts)[1])) {
     plot(X[, plts[i, ]], pch=20, cex=.5, xlab="", ylab="", axes=FALSE, frame=TRUE)
     title(xlab=paste("col", plts[i, 1]), ylab=paste("col", plts[i, 2]), line=0)
}
par(oldp)

enter image description here

dcarlson
  • 10,936
  • 2
  • 15
  • 18