Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color palette for GGally::ggpairs?

Tags:

r

ggally

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

like image 342
Make42 Avatar asked Jan 12 '16 09:01

Make42


2 Answers

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

like image 100
user20650 Avatar answered Oct 23 '22 17:10

user20650


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'))
like image 2
Rodrigo San Cristóbal Blanco Avatar answered Oct 23 '22 17:10

Rodrigo San Cristóbal Blanco