ggplot2/GGally中的散点矩阵,无密度图 [英] Scattermatrix in ggplot2/GGally without density plots

查看:86
本文介绍了ggplot2/GGally中的散点矩阵,无密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下代码用ggally创建了一个ggplot2扩展名的散点图

I made a scattermatrix with the ggplot2 extension GGally with the following code

  ggscatmat(dat2, columns = 2:6, color="car", alpha=0.8) +
  ggtitle("Korrelation") + 
  theme(axis.text.x = element_text(angle=-40, vjust=1, hjust=0, size=10))

现在我的问题是,在这种情况下,我实际上并不需要密度线图或相关系数.我只想要矩阵中的散点图.有没有办法删除"其他方面?我无法在文档中找到任何内容.

Now my problem is that in this case I don't really need the density lineplot or the correlation coeff., I do only want the scatterplots in the matrix. Is there a way to "delete" the other facets? I can#T find anything in the documentation.

请原谅我的英语不好,谢谢您的任何建议或帮助!

Please excuse my bad english and thanks for any advice or help!

我发现ggpairs并不是一个完美的解决方案:

I found a not yet perfect solution with ggpairs:

ggpairs(dat2, columns = 2:6, mapping= aes(color=car), 
        upper = "blank",diag = "blank") +
  theme(axis.text.x = element_text(angle=-40, vjust=1, hjust=0, size=10))

但是现在没有图例了,两个标签看起来还没有完全加载:

But now there's no legend anymore and two labels looking like the plot hasn't fully loaded yet:

推荐答案

您可以通过弄乱gtable

 removePanels <- function(plot) {

         g <-  ggplotGrob(plot)

         # get panels to remove: upper + diagonal
         ids <- grep("panel", g$layout$name)
         cols <- sqrt(diff(range(ids)) +1)
         remove <- matrix(ids, ncol=cols)
         remove <- remove[upper.tri(remove, diag=TRUE)]

         # remove certain axis
         yax <- grep("axis-l", g$layout$name)[1] # first
         xax <- tail(grep("axis-b", g$layout$name), 1) #last

         # remove cetain strips
        ystrip <- grep("strip-right", g$layout$name)[1]
        xstrip <- tail(grep("strip-top", g$layout$name), 1)

        # remove grobs
        g$grobs[c(remove, xax, yax, ystrip, xstrip)] <- NULL
        g$layout <- g$layout[-c(remove, xax, yax, ystrip, xstrip),]
        g
      }

# draw
library(GGally)
library(ggplot2)
library(grid)

p <- ggscatmat(iris, columns = 1:4, color="Species", alpha=0.8) +
          theme(axis.text.x = element_text(angle=-40, vjust=1, hjust=0, size=10))

grid.newpage()      
grid.draw(removePanels(p))

这篇关于ggplot2/GGally中的散点矩阵,无密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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