r - 如何在 ggpairs [GGally] 中自定义线条

标签 r ggplot2 ggally

我有以下情节:

使用此代码生成:

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

我的问题是:
  • 如何将相关线更改为 red ,现在它是黑​​色的。
  • 和相关线埋在散点图之下。我想把它放在上面。我怎样才能做到这一点?
  • 最佳答案

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

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

    关于r - 如何在 ggpairs [GGally] 中自定义线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858337/

    相关文章:

    r - 如何在错误时暂停 R 脚本

    r - x 轴为周数,次要 x 轴为日期

    r - 如何使用 ggpairs() 在不同方面设置相同的比例

    r - 如何用 GGally::ggpairs 制作气泡图?

    r - 如何在 ggplot2 生成 Kaplan-Meier 图的置信区间中添加阴影和颜色?

    r - 倒置发散调色板

    r - 如何计算leap年?

    r - ggplot2 boxplot中的标签框

    r - ggplot 上的抛物线引用线

    r - biglm 预测无法分配大小为 xx.x MB 的向量