有没有办法使用ggplot更改GGally

有没有办法使用ggplot更改GGally

本文介绍了有没有办法使用ggplot更改GGally :: ggpairs的调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 使用

  • aes_string()




  • 让我们试试这个:

      ggpairs(钻石[,1:2],颜色='cut')


    非常好,我们快到了!我们只需要覆盖调色板。请注意,像你这样的建议

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

    将不起作用,因为ggpairs对象不是ggplot,所以 + 表示法不以任何方式直接适用。不过,这里提供了简单的解决方法。交叉手指,并... ...

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

    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 getPlot, the colors do not change.

    my_pair_plot = ggpairs(dataset, color="var1")
    getPlot(my_pair_plot,2,1) + scale_fill_brewer(palette = "Set2")
    

    Attempting to put ggplot commands directly on the ggpairs function results in an error.

    ggpairs(dataset, color="var1") + scale_fill_brewer(palette = "Set2")
    
    解决方案

    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')
    

    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')
    

    这篇关于有没有办法使用ggplot更改GGally :: ggpairs的调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-05 20:35