9

This is the same question as in User defined colour palette in R and ggpairs or is there a way to change the color palette for GGally::ggpairs using ggplot?

only that the solutions there don't work anymore.


I also want to change the color palette, but is there a way to change the color palette for GGally::ggpairs using ggplot? does not work anymore. What to do?

MWE:

library(GGally)
library(ggplot2)
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]
ggpairs(
  diamonds.samp[,1:2],
  mapping = ggplot2::aes(color = cut),
  upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
  lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
  diag = list(continuous = wrap("densityDiag")),
  title = "Diamonds"
)

enter image description here

I would like to add

scale_colour_manual(values=c('red','blue','green','red','blue'))

(obviously that is just dummy code) and get something like (I did not paint over all the dots):

enter image description here

Community
  • 1
  • 1
Make42
  • 12,236
  • 24
  • 79
  • 155
  • 4
    Please don't be so lazy and provide at least an MVE. –  Jan 12 '16 at 09:49
  • @Pascal: Sorry, forgot a line. Please try again. – Make42 Jan 12 '16 at 10:06
  • What is your expected output? Because I get colored figures with your code. –  Jan 12 '16 at 10:10
  • @Pascal: Of course :-). I would like to *change* the colors that are set automatically. I give a (very ugly) example. – Make42 Jan 12 '16 at 10:31
  • Still not clear how it is (was) supposed to work. –  Jan 12 '16 at 10:44
  • 1
    @Pascal: If you run the code you see in the MWE, you get the first plot. I am sure that you get that plot as well, right? The colors here are red, some-sand-yellow, green, blue, mangenta, right? I want to change theses colors. For the example I don't care to which colors. An example would be 'red','blue','green','red','blue'. I am not sure where the example is unclear. – Make42 Jan 12 '16 at 10:47
  • 1
    This will **not** be the way to do it, *but it is a way* ... just overwrite each plot in the matrix... `for(i in 1:2) { for(j in 1:2){ p[i,j] <- p[i,j] + scale_fill_manual(values=c("red", "blue", "green", "yellow", "black")) + scale_color_manual(values=c("red", "blue", "green", "yellow", "black")) } }` – user20650 Jan 12 '16 at 12:05
  • @user20650: I had a similar solution (even a bit more ugly), before it broke with the new version of GGally. I guess your solution is good enough for me: Iit won't get more elegant than that. If you like, please put it as an answer. – Make42 Jan 12 '16 at 12:12
  • okay done. There must be a way to do this within ggpairs, but i am not at ll familiar with it.... so good enough – user20650 Jan 12 '16 at 12:22
  • @user20650: I don't think there is. GGally is very hard to configure - you always ends up hacking. – Make42 Jan 12 '16 at 12:25

2 Answers2

15

One solution is to extract each plot from the ggmatrix add a new scale_ and then reassign it back to the matrix.

Example

library(GGally)
library(ggplot2)
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1],200),]

p <- ggpairs(
  diamonds.samp[,1:2],
  mapping = ggplot2::aes(color = cut),
  upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
  lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
  diag = list(continuous = wrap("densityDiag")),
  title = "Diamonds"
)

Loop through each plot changing relevant scales

for(i in 1:p$nrow) {
  for(j in 1:p$ncol){
    p[i,j] <- p[i,j] + 
        scale_fill_manual(values=c("red", "blue", "green", "yellow", "black")) +
        scale_color_manual(values=c("red", "blue", "green", "yellow", "black"))  
  }
}

p

To give

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
2

Adding directly both + scale_fill_manual and + scale_colour_manual after your ggpairs code will work also:

ggpairs(
  diamonds.samp[,1:2],
  mapping = ggplot2::aes(color = cut),
  upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
  lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot", alpha = 0.4)),
  diag = list(continuous = wrap("densityDiag")),
  title = "Diamonds") +    
    scale_fill_manual(values=c('red','blue','green','red','blue')) +
    scale_colour_manual(values=c('red','blue','green','red','blue'))