如何自定义 ggpairs 中的线条 [GGally]

8
我有以下的图表:

enter image description here

这个图表是使用下面的代码生成的:
library("GGally")
data(iris)
ggpairs(iris[, 1:4], lower=list(continuous="smooth", params=c(colour="blue")),
  diag=list(continuous="bar", params=c(colour="blue")), 
  upper=list(params=list(corSize=6)), axisLabels='show')

我的问题是:

  1. 如何将相关线改为红色,现在它是黑色的。
  2. 相关线被散点图覆盖了。我想把它放在最上面。怎么做?

你可以通过将alpha=0.3添加到散点图的较低列表参数中来控制点的透明度。这将有助于更加关注平滑的线条。 - nehiljain
@nehiljain:不,那行不通。当散点图密集时,线仍然会被埋藏。我想的是这条线。但是不知道如何在ggpairs中实现它。 - neversaint
2个回答

15

检查您的GGally版本,方法是运行packageVersion("GGally"),如果版本低于1.0.1,请升级您的GGally。

library("GGally")
library("ggplot2")
data(iris)

lowerFn <- function(data, mapping, method = "lm", ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(colour = "blue") +
    geom_smooth(method = method, color = "red", ...)
  p
}

ggpairs(
  iris[, 1:4], lower = list(continuous = wrap(lowerFn, method = "lm")),
  diag = list(continuous = wrap("barDiag", colour = "blue")),
  upper = list(continuous = wrap("cor", size = 10))
)

在此输入图片描述


3

我希望有更简单的方法来做这件事情,但这是一种比较粗暴的方法。但这确实能够让你更加灵活地自定义图表。主要就是通过使用putPlot将一个ggplot2的绘图放入到图中。

library(ggplot2)

## First create combinations of variables and extract those for the lower matrix
cols <- expand.grid(names(iris)[1:4], names(iris)[1:3])    
cols <- cols[c(2:4, 7:8, 12),]  # indices will be in column major order

## These parameters are applied to each plot we create
pars <- list(geom_point(alpha=0.8, color="blue"),              
             geom_smooth(method="lm", color="red", lwd=1.1))

## Create the plots (dont need the lower plots in the ggpairs call)
plots <- apply(cols, 1, function(cols)                    
    ggplot(iris[,cols], aes_string(x=cols[2], y=cols[1])) + pars)
gg <- ggpairs(iris[, 1:4],
              diag=list(continuous="bar", params=c(colour="blue")), 
              upper=list(params=list(corSize=6)), axisLabels='show')

## Now add the new plots to the figure using putPlot
colFromRight <- c(2:4, 3:4, 4)                                    
colFromLeft <- rep(c(1, 2, 3), times=c(3,2,1))
for (i in seq_along(plots)) 
    gg <- putPlot(gg, plots[[i]], colFromRight[i], colFromLeft[i])
gg

enter image description here

## If you want the slope of your lines to correspond to the 
## correlation, you can scale your variables
scaled <- as.data.frame(scale(iris[,1:4]))
fit <- lm(Sepal.Length ~ Sepal.Width, data=scaled)
coef(fit)[2]
# Sepal.Length 
#  -0.1175698 

## This corresponds to Sepal.Length ~ Sepal.Width upper panel

编辑

为了将其泛化为一个接受任何列索引并制作相同图形的函数

## colInds is indices of columns in data.frame
.ggpairs <- function(colInds, data=iris) {
    n <- length(colInds)
    cols <- expand.grid(names(data)[colInds], names(data)[colInds])
    cInds <- unlist(mapply(function(a, b, c) a*n+b:c, 0:max(0,n-2), 2:n, rep(n, n-1)))
    cols <- cols[cInds,]  # indices will be in column major order

    ## These parameters are applied to each plot we create
    pars <- list(geom_point(alpha=0.8, color="blue"),              
                 geom_smooth(method="lm", color="red", lwd=1.1))

    ## Create the plots (dont need the lower plots in the ggpairs call)
    plots <- apply(cols, 1, function(cols)                    
        ggplot(data[,cols], aes_string(x=cols[2], y=cols[1])) + pars)
    gg <- ggpairs(data[, colInds],
                  diag=list(continuous="bar", params=c(colour="blue")), 
                  upper=list(params=list(corSize=6)), axisLabels='show')

    rowFromTop <- unlist(mapply(`:`, 2:n, rep(n, n-1)))
    colFromLeft <- rep(1:(n-1), times=(n-1):1)
    for (i in seq_along(plots)) 
        gg <- putPlot(gg, plots[[i]], rowFromTop[i], colFromLeft[i])
    return( gg )
}

## Example
.ggpairs(c(1, 3))

@Legalizelt:我该如何将你的代码泛化为一个函数,以便它可以处理任意数量列的数据?(即现在你假设有4列) - neversaint
1
这不应该太难了,只需要将索引泛化一下,我可以试一试。 - Rorschach
@Legalizelt:你是不是指的是rowFromTop而不是colFromRight - neversaint
1
你可以尝试一下那个函数。 - Rorschach

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接