is there a way to change the color palette for GGally::ggpairs using ggplot?

后端 未结 3 1037
后悔当初
后悔当初 2020-12-30 03:37

I would like to change the color palette for the GGally function ggpairs. When I attempt to add ggplot commands to the ggplot returned using

3条回答
  •  长发绾君心
    2020-12-30 04:23

    Turns out, this is possible! It requires looking up the source code, but the solution comes up pretty easily. We are interested in ggpairs function, so the first step is just

    ggpairs
    

    Let's see if we can find any aes mapping to fill or colour. Indeed,

    combo_aes <- addAndOverwriteAes(aes_string(x = xColName, 
                y = yColName, ...), section_aes)
    

    We may hope it does what it says. Two important notes:

    • colour and fill aes should be contained in the ellipsis for the ggpairs call

    • aes_string() is used

    Let's try this out:

    ggpairs(diamonds[, 1:2], colour='cut')
    

    enter image description here

    Excellent, we're almost there! We just need to override the colour palette. Note that something like you propose

    ggpairs(diamonds[, 1:2], colour='cut') + scale_fill_brewer(palette = "Set2")
    

    will not work because ggpairs object is not a ggplot, so the + notation is not directly applicable in any way. However, the simple workaround is provided here. Cross your fingers, and...

    ggplot <- function(...) ggplot2::ggplot(...) + scale_fill_brewer(palette="Set2")
    ggpairs(diamonds[, 1:2], colour='cut')
    

    enter image description here

提交回复
热议问题