【问题标题】:How to customize lines in ggpairs [GGally]如何自定义 ggpairs 中的行 [GGally]
【发布时间】:2015-08-31 17:25:27
【问题描述】:

我有以下情节:

使用此代码生成:

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. 如何将关联线更改为red,现在它是黑​​色的。
  2. 并且相关线隐藏在散点图下方。我想把它放在上面。我该怎么做?

【问题讨论】:

  • 您可以通过将alpha=0.3 添加到下方列表参数中来控制散点图中点的透明度。这将有助于更加集中流畅的线条。
  • @nehiljain:不,那不行。当散点图密集时,线仍将被掩埋。我在想this line。但不知道如何在ggpairs中实现。

标签: r ggplot2 ggally


【解决方案1】:

通过 packageVersion("GGally") 检查您的 GGally 版本并将您的 GGally 升级到 1.0.1 版

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

【讨论】:

    【解决方案2】:

    我希望有一种更简单的方法可以做到这一点,但这是一种蛮力方法。但是,它确实为您提供了进一步轻松自定义绘图的灵活性。重点是使用putPlotggplot2 plot 放入图中。

    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
    

    ## 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 列)
    • 应该不会太难,指数只是需要概括,我可以试一试
    • @Legalizelt: colFromRight 你的意思是rowFromTop
    • 你可以试试这个功能
    猜你喜欢
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2015-05-06
    • 2015-04-10
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多