散矩阵在ggplot2/GGally没有密度图

时间:2021-08-01 14:58:27

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

使用以下代码,我使用ggplot2扩展生成了一个散矩阵

  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!

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

散矩阵在ggplot2/GGally没有密度图

Edit: I found a not yet perfect solution with ggpairs:

编辑:我发现了一个尚未完善的ggpair解决方案:

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: 散矩阵在ggplot2/GGally没有密度图

但现在已经没有什么传奇了,有两家公司的剧情似乎还没有完全成型:

2 个解决方案

#1


4  

You can manually remove parts of the plot by messing about with the gtable

您可以手动删除部分阴谋通过混乱的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))

#2


3  

Following the official documentation, you can set an element of the ggpairs to blank. In your case you would be interested in changing the value for the diag to diag = "blank" , as shown in the example below.

根据官方文档,您可以将ggpair的一个元素设置为blank。在您的示例中,您可能想要将diag的值更改为diag = "blank",如下面的示例所示。

Example

On example of the mtcars data, you could do the following:

在mtcars数据的示例中,您可以执行以下操作:

data("mtcars")
require(GGally)
ggpairs(data = mtcars[3:5], diag = "blank")

Results

The code would produce the desired chart without the diagonal plot: 散矩阵在ggplot2/GGally没有密度图

代码将生成所需的图表,而不包含对角线图:

#1


4  

You can manually remove parts of the plot by messing about with the gtable

您可以手动删除部分阴谋通过混乱的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))

#2


3  

Following the official documentation, you can set an element of the ggpairs to blank. In your case you would be interested in changing the value for the diag to diag = "blank" , as shown in the example below.

根据官方文档,您可以将ggpair的一个元素设置为blank。在您的示例中,您可能想要将diag的值更改为diag = "blank",如下面的示例所示。

Example

On example of the mtcars data, you could do the following:

在mtcars数据的示例中,您可以执行以下操作:

data("mtcars")
require(GGally)
ggpairs(data = mtcars[3:5], diag = "blank")

Results

The code would produce the desired chart without the diagonal plot: 散矩阵在ggplot2/GGally没有密度图

代码将生成所需的图表,而不包含对角线图: