144

Is it possible to plot a matrix of scatter plots with ggplot2, using ggplot's nice features like mapping additional factors to color, shape etc. and adding smoother?

I am thinking about something similar to the base function pairs.

epo3
  • 2,991
  • 2
  • 33
  • 60
Karsten W.
  • 17,826
  • 11
  • 69
  • 103

4 Answers4

259

I keep wanting to do this, but plotmatrix is crap. Hadley recommends using the GGally package instead. It has a function, ggpairs that is a vastly improved pairs plot (lets you use non-continuous variables in your data frames). It plots different plots in each square, depending on the variable types:

library(GGally)
ggpairs(iris, aes(colour = Species, alpha = 0.4))

enter image description here

jciloa
  • 1,039
  • 1
  • 11
  • 22
naught101
  • 18,687
  • 19
  • 90
  • 138
  • 31
    This is really great. It is worth noting that any `colour` variable has to be a factor; spent 45 minutes figuring that one out. – gregmacfarlane Oct 28 '13 at 18:21
  • +1. By the way, do you know how to adjust the plots so that the labels are shown in their entirety and not overlapping? – Steve S Sep 27 '14 at 03:14
  • 1
    Is there any way to plot these correlation matrices without pairs?For example I need to plot the first column versus every other column.ggpairs gives very possible pair.For e.g I have 10 columns and it gives me 10 *10 =100.But I want 1st column vs other 9 only – Rgeek Feb 05 '15 at 21:30
  • 1
    @Rgeek: you could `melt` the dataframe using the variable you are interested in as an id variable, and then facet by the other variables. – naught101 Feb 05 '15 at 23:26
  • So now my data is in the required format with 3 columns.First being the id ,second being the variable and 3rd being the value of the variable.The first is repeated according to the number of variables which are 10.I can do ggplot but not sure how to do ggpairs on this as I need 10 correlation plots. – Rgeek Feb 06 '15 at 19:33
  • 8
    Don't use ggpairs, just use ggplot2 normally, like `ggplot(data, aes(x=id, y=value)) + geom_point() + facet_grid(.~variable)`. I'm assuming you're talking about scatter plots when you say "correlation plot", because I've never heard of that otherwise. – naught101 Feb 07 '15 at 12:23
  • got following warning and no color in the plot. `In ggpairs(iris, colour = "Species", alpha = 0.4) : Extra arguments: 'colour', 'alpha' are being ignored. If these are meant to be aesthetics, submit them using the 'mapping' variable within ggpairs with ggplot2::aes or ggplot2::aes_string.` any idea how to make it work with the newest ggplot2?.. – Jim Green May 05 '16 at 05:36
  • 3
    @JimGreen `GGally::ggpairs(iris, aes(colour = Species, alpha=0.4))` – Alejandro Alcalde May 05 '16 at 09:35
40

You might want to try plotmatrix:

  library(ggplot2)
  data(mtcars)
  plotmatrix(mtcars[,1:3])

to me mpg (first column in mtcars) should not be a factor. I haven't checked it, but there's no reason why it should be one. However I get a scatter plot :)


Note: For future reference, the plotmatrix() function has been replaced by the ggpairs() function from the GGally package as @naught101 suggests in another response below to this question.

Community
  • 1
  • 1
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • I could not get faceting to work on this problem, it seems it requires factors on the right hand side of the formula.. Or could you give me a minimal example? – Karsten W. Sep 17 '10 at 12:44
  • 1
    Anyone know how to add colour? I don't seem to be able to get this https://gist.github.com/1405150 to work – Etienne Low-Décarie Apr 30 '12 at 15:26
  • 1
    The first part of this answer is wrong, and cause for confusion. You can't do pairs plots with faceting: you can only do y by x plots, and group them by factors. In other words, with faceting you have the same x and y on each sub-plot; with pairs, you have a different x on each column, and a different y on each row. – naught101 Aug 21 '12 at 02:14
  • 32
    For future reference, the `plotmatrix()` function has been replaced by the `ggpairs()` function from the `GGally` package as @naught101 suggests in another response to this question. – smillig May 14 '13 at 12:27
  • Y'all down voters realize that this answer is almost 5 years old? Do you want me to delete / edit this answer? – Matt Bannert Jun 24 '15 at 13:03
  • 1
    @MattBannert added the comment to your post to make it more visible, I could understand the downvoters if had skipped reading the comments. Hope you don't mind. – zx8754 Sep 05 '16 at 06:19
21

If one wants to obtain a ggplot object (not ggmatrix as in case of ggpairs()), the solution is to melt the data twice, then ggplot with facetting. facet_wrap would be better than facet_grid in limiting the plotted area, given the scales = 'free' parameter is supplied.

require(ggplot2) 
require(dplyr)
require(tidyr)

gatherpairs <- function(data, ..., 
                        xkey = '.xkey', xvalue = '.xvalue',
                        ykey = '.ykey', yvalue = '.yvalue',
                        na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
  vars <- quos(...)
  xkey <- enquo(xkey)
  xvalue <- enquo(xvalue)
  ykey <- enquo(ykey)
  yvalue <- enquo(yvalue)

  data %>% {
    cbind(gather(., key = !!xkey, value = !!xvalue, !!!vars,
                 na.rm = na.rm, convert = convert, factor_key = factor_key),
          select(., !!!vars)) 
  } %>% gather(., key = !!ykey, value = !!yvalue, !!!vars,
               na.rm = na.rm, convert = convert, factor_key = factor_key)
}

iris %>% 
  gatherpairs(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) %>% {
  ggplot(., aes(x = .xvalue, y = .yvalue, color = Species)) +
      geom_point() + 
      geom_smooth(method = 'lm') +
      facet_wrap(.xkey ~ .ykey, ncol = length(unique(.$.ykey)), scales = 'free', labeller = label_both) +
      scale_color_brewer(type = 'qual')
}

enter image description here

mjktfw
  • 840
  • 6
  • 14
4

Try scatterPlotMatrix. It's very flexible and produces nice looking interactive charts.

library(scatterPlotMatrix)
scatterPlotMatrix(iris, zAxisDim = "Species")

enter image description here

epo3
  • 2,991
  • 2
  • 33
  • 60