现在参数已经过时,改变ggpairs中的颜色 [英] Change colors in ggpairs now that params is deprecated

查看:3024
本文介绍了现在参数已经过时,改变ggpairs中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这些帖子


我认为这是一个很好的结果,但我无法改变颜色。



一个MWE就是这个

  library(ggally)

#载入破解
源码(ggally_mod.R)
#I s aved https://github.com/tonytonov/ggally/blob/master/R/gg-plots.r asggally_mod.R
assignInNamespace(ggally_cor,ggally_cor,GGally)

ggpairs(瑞士)

现在我想运行

  ggpairs(swiss,
lower = list(continuous =smooth,wrap = c(color =blue)),
diag = list(continuous =bar,wrap = c(color =blue)))

但颜色保持不变。有没有办法改变颜色,现在的参数不工作了? 您没有使用正确包装 -
详细信息参见小插曲。同样对于对角线,你现在必须使用 barDiag (但是 ggpairs 给出了非常有用的错误来说明这一点)



因此,对于您的示例,我们可以更改
下部面板中点的颜色 fill 下面的横条

  library(GGally)
library(ggplot2)
ggpairs(swiss [1:3],
lower = list(continuous = wrap(smooth,color =blue)),
diag = list(continuous = wrap(barDiag,fill =blue)))

然而,平滑是硬编码(请参阅 ggally_smooth ),要更改其
颜色,您需要定义您自己的要传递的函数。所以从这里

  my_fn < - 函数(data,mapping,pts = list(),smt = list(),...){ 
ggplot(data = data,mapping = mapping,...)+
do.call(geom_point,pts)+
do.call(geom_smooth,smt)
}

#Plot
ggpairs(swiss [1:4],
lower = list(continuous =
wrap(my_fn,
pts = list(size = 2,color =red),
smt = list(method =lm,se = F,size = 5,color =blue))),
diag = list(continuous = wrap(barDiag,fill =blue)))






以类似的方式,这里定义一个新的上部相关函数(类似于你所拥有的)

  cor_fun<  - 好玩ction(data,mapping =pearson,ndp = 2,sz = 5,stars = TRUE,...){

data < - na.omit(data [,c( as.character(mapping $ x),as.character(mapping $ y))])

x< - data [,as.character(mapping $ x)]
y< - 数据[,as.character(mapping $ y)]

corr < - cor.test(x,y,method = method)
est < - corr $ estimate $ b $ (星号){
星号< -c(***,**,*)bb.size <-sz * abs(est)

,)[findInterval(corr $ p.value,c(0,0.001,0.01,0.05,1))]
lbl < - paste0(round(est,ndp),stars)
}其他{
lbl< - round(est,ndp)
}

ggplot(data = data,mapping = mapping)+
annotate(text ,x = mean(x),y​​ = mean(y),label = lbl,size = lb.size,...)+
theme(panel.grid = element_blank())
}


ggpairs(swiss,
lower = list(continuous = wrap(smooth,color =blue)),
diag = list(continuous = wrap (barDiag,fill =blue)),
upper = list(continuous = cor_fun))


I saw these posts GGally::ggpairs plot without gridlines when plotting correlation coefficient use ggpairs to create this plot

After reading I was able to implement this hack https://github.com/tonytonov/ggally/blob/master/R/gg-plots.r and my plot looks like this

I think this is a good result but I cannot change the colors.

A MWE is this

library(ggally)

# load the hack
source("ggally_mod.R") 
# I saved https://github.com/tonytonov/ggally/blob/master/R/gg-plots.r as "ggally_mod.R"
assignInNamespace("ggally_cor", ggally_cor, "GGally")

ggpairs(swiss)

Now I want to run

ggpairs(swiss, 
 lower=list(continuous="smooth", wrap=c(colour="blue")),
 diag=list(continuous="bar", wrap=c(colour="blue")))

But the colours remain the same. Is there a way to change the colours now that params is not working anymore?

解决方案

You are not using wrap correctly - see the vignette for details. Also for the diagonal you now have to use the function barDiag (but ggpairs gives very helpful errors to tell this)

So for your example, we can change the colour of the points in the lower panels and the fill of the bars below

library(GGally)
library(ggplot2)
ggpairs(swiss[1:3], 
        lower=list(continuous=wrap("smooth", colour="blue")),
        diag=list(continuous=wrap("barDiag", fill="blue")))

However, as the colour of the smooth is hard coded (see ggally_smooth), to change its colour you need to define you own function to pass. So from here

my_fn <- function(data, mapping, pts=list(), smt=list(), ...){
              ggplot(data = data, mapping = mapping, ...) + 
                         do.call(geom_point, pts) +
                         do.call(geom_smooth, smt) 
                 }

# Plot 
ggpairs(swiss[1:4], 
        lower = list(continuous = 
                       wrap(my_fn,
                            pts=list(size=2, colour="red"), 
                            smt=list(method="lm", se=F, size=5, colour="blue"))),
                     diag=list(continuous=wrap("barDiag", fill="blue")))


In a similar way, here is a way to define a new upper correlation function (similar to what you have)

cor_fun <- function(data, mapping, method="pearson", ndp=2, sz=5, stars=TRUE, ...){

    data <- na.omit(data[,c(as.character(mapping$x), as.character(mapping$y))])

    x <- data[,as.character(mapping$x)]
    y <- data[,as.character(mapping$y)]

    corr <- cor.test(x, y, method=method)
    est <- corr$estimate
    lb.size <- sz* abs(est) 

    if(stars){
      stars <- c("***", "**", "*", "")[findInterval(corr$p.value, c(0, 0.001, 0.01, 0.05, 1))]
      lbl <- paste0(round(est, ndp), stars)
    }else{
      lbl <- round(est, ndp)
    }

    ggplot(data=data, mapping=mapping) + 
      annotate("text", x=mean(x), y=mean(y), label=lbl, size=lb.size,...)+
      theme(panel.grid = element_blank())
  }


ggpairs(swiss, 
        lower=list(continuous=wrap("smooth", colour="blue")),
        diag=list(continuous=wrap("barDiag", fill="blue")),
        upper=list(continuous=cor_fun))

这篇关于现在参数已经过时,改变ggpairs中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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