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

后端 未结 1 728
南方客
南方客 2021-01-12 06:54

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 tha

相关标签:
1条回答
  • 2021-01-12 07:25

    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

    0 讨论(0)
提交回复
热议问题