有没有办法使用ggplot更改GGally :: ggpairs的调色板? [英] is there a way to change the color palette for GGally::ggpairs using ggplot?

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

问题描述

我想更改 GGally 函数 ggpairs 的调色板。当我尝试将ggplot命令添加到使用 getPlot 返回的ggplot时,颜色不会改变。

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

试图将ggplot命令直接放在 ggpairs 函数上会导致错误。

  ggpairs(dataset,color =var1)+ scale_fill_brewer(palette =Set2)


解决方案

原来,这是可能的!它需要查找源代码,但解决方案非常容易。我们对 ggpairs 函数感兴趣,所以第一步就是

  ggpairs 

让我们看看我们是否可以找到任何aes映射来填充或着色。的确,

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

我们可能希望它能够做到。两个重要注释:


  • 颜色和填充aes应该包含在 ggpairs的省略号中

    code> call
  • 使用
  • 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的调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆