1

I'm trying to create a scatterplot matrix where the x and y variables of the matrix are different (as opposed to pairs() which use the same variables for both x and y axes).

Currently I'm using pairs2() function from the TeachingDemos library, but would like to have more control to its appearance and am thinking of replicating it using ggplot2. Is there something equivalent in ggplot2? Or how best should I produce a similar scatterplot matrix if I don't want to use the pairs2() function?

Not sure if I need to post sample code, here it is anyway

library(TeachingDemos)
a <- matrix(runif(1:10), ncol = 2)
b <- matrix(sample(1:10), ncol = 2)
pairs2(a,b)

Edit:

to clarify, a and b are both matrixes of 2 different variables each. May be clearer with the following sample code instead

library(TeachingDemos)
a <- matrix(runif(1:10), ncol = 2)
b <- matrix(sample(1:10), ncol = 2)
colnames(a) <- c("Ratio of correct answer", "Ratio of time spent")
colnames(b) <- c("Hours spent per study session", "Frequency of study per week")
pairs2(a,b)

Thanks Henrik for the suggestion. Apologies on uncear descriptions, a learning noob here, all suggestions welcome.

Ricky
  • 4,616
  • 6
  • 42
  • 72
  • 1
    @Henrik I don't think that is a good duplicate; it is for `pairs()`, not `pairs2()`. – Brian Diggs Mar 04 '14 at 00:05
  • Thanks for your comment Brian. I turn my close vote into a recommendation instead. @Ricky, you may find [**this post**](http://stackoverflow.com/questions/3735286/pairs-equivalent-in-ggplot2) helpful. – Henrik Mar 04 '14 at 00:14
  • Thanks Henrik. As Brian remarked, that post (which I've seen) addresses pairs(), not pairs2() – Ricky Mar 04 '14 at 05:53

1 Answers1

0

As is often the case with ggplot2, the hard part is getting the data into the correct form. In this case we want to collapse the two x-variable columns into one, collapse the two y-variable columns into one, and add indicators that denote which x and y variable each value comes from:

library(reshape2)
library(ggplot2)    
dat <- data.frame(a, b)
    names(dat) <- c("x_1", "x_2", "y_1", "y_2")
    dat.m <- melt(dat, measure.vars = c("x_1", "x_2"), variable.name = "x_var", value.name = "x")
    dat.m <- melt(dat.m, measure.vars = c("y_1", "y_2"), variable.name = "y_var", value.name = "y")

Now that the data is in the form expected by ggplot2, constructing the actual graphic is easy:

ggplot(dat.m, aes(x=x, y=y)) +
    geom_point()+
    facet_grid(y_var ~ x_var, scales="free")

enter image description here

EDIT: An updated version corresponding to your new example is

library(reshape2)
library(ggplot2)    

dat <- data.frame(a, b, check.names = FALSE)

dat.m <- melt(dat,
              measure.vars = colnames(a),
              variable.name = "x_var",
              value.name = "Ratio")

dat.m <- melt(dat.m,
              measure.vars = colnames(b),
              variable.name = "y_var",
              value.name = "Study")

ggplot(dat.m, aes(x=Ratio, y=Study)) +
    geom_point()+
    facet_grid(y_var ~ x_var, scales="free")

enter image description here

Ista
  • 10,139
  • 2
  • 37
  • 38
  • Thanks Ista, but I don't want to facet. What your answer provided are 4 scatterplots of variables x vs y, but grouped within facets like [x_1, y_1] etc. What I'm looking for is 4 scatterplots where the variables are x_1 vs y_1, x_1 vs y_2, x_2 vs y_1, and x_2 vs y_2. There are no variables x and y. Maybe easier to conceptualize if I put a name to the variables; imagine x_1 and x_2 are inputs e.g. "Study Duration" and "Study Frequency", and y_1 and y_2 are outputs e.g. "Grades" and "Fatigue". I'm trying to do pairs of inputs/outputs, but not inputs/inputs and outputs/outputs that pairs() do – Ricky Mar 04 '14 at 06:00
  • @Ricky, I think you may consider adding this information to your question - it tend to 'disappear' in the comments, and it is much easier to format nicely in the question. You may also add the `dimnames` to the example matrices in your question. It might be slightly easier to track the variables and how you wish to 'map' the matrix to the plot. Cheers. – Henrik Mar 04 '14 at 07:58
  • @Ricky -- Huh? My answer gives exactly the same output as `pairs2(a, b)`. You can remove the labels from the x and y axes is you prefer. – Ista Mar 04 '14 at 11:34
  • @Ricky -- I've updated the answer to correspond to your updated example, but the basic answer is still the same. – Ista Mar 04 '14 at 14:37