Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

r - How to show only the lower triangle in ggpairs?

I am using ggpairs to make a pairs plot, but I only want to display the lower triangle. I can make the diagonal and upper triangle blank, but cannot make them go, which leaves an empty row and an empty column which I don't want.

Any suggestions?

library("GGally")
ggpairs(iris[, 1:4], 
        lower  = list(continuous = "points"),
        upper  = list(continuous = "blank"),
        diag  = list(continuous = "blankDiag")
        )

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ggpairs object can be edited. The bulk of the object is list of plots. The unwanted plots can be removed from this list and the other elements of the ggpairs object modified to match.

Here is a function that will do this

gpairs_lower <- function(g){
  g$plots <- g$plots[-(1:g$nrow)]
  g$yAxisLabels <- g$yAxisLabels[-1]
  g$nrow <- g$nrow -1

  g$plots <- g$plots[-(seq(g$ncol, length(g$plots), by = g$ncol))]
  g$xAxisLabels <- g$xAxisLabels[-g$ncol]
  g$ncol <- g$ncol - 1

  g
}

library("GGally")
g <- ggpairs(iris[, 1:4], 
             lower  = list(continuous = "points"),
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag")
     )

gpairs_lower(g)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...