5

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!

Scattermatrix with ggscatmat {GGally}

Edit: 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: enter image description here

Marie-Louise
  • 419
  • 1
  • 5
  • 18
  • If `p` is your `ggscatmat` you can remove specific parts of the plot vy messing with the `gtable`: an example `g = ggplotGrob(p); g$grobs[c(10, 14)] <- NULL; g$layout <- g$layout[-c(10, 14),]; grid::grid.newpage(); grid::grid.draw(g)` – user20650 Apr 20 '16 at 11:57
  • This is great! But how do I know which part is in which vector? For example, c(10,14) seems to be the third scatterplot in the first column, so why 10 and 14? And can I delete more than one part at once? gtable is new to me – Marie-Louise Apr 20 '16 at 12:06
  • http://stackoverflow.com/questions/28427572/manipulating-axis-titles-in-ggpairs-ggally/29322578#29322578 shows one way to remove the extra labels in ggpairs – user20650 Apr 20 '16 at 12:19

2 Answers2

6

You can manually remove parts of the plot by messing about with the 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))
user20650
  • 24,654
  • 5
  • 56
  • 91
  • 1
    This is just perfect! Is it possible to delete the two superfluous lables, too? – Marie-Louise Apr 20 '16 at 12:19
  • Good stuff. Do you mean the empty space where the strip labels were removed? What would you like to do? Would you prefer just to remove the labels but keep the grey box? – user20650 Apr 20 '16 at 12:36
  • 1
    I guess it isn't possible to just push the grey label boxes on step closer to the plots, is it? It would be nice but I am really happy with the result anyway. Thank you very, very much! – Marie-Louise Apr 20 '16 at 12:38
  • You're very welcome. Hmm im not sure - probably need to really get into the gtable stuff (which im quite rubbish with). A naive way is this, but looks fairly poor. `g <- removePanels(p); idx <- grep("strip-right", g$layout$name); g$layout[idx, c("l", "r")] <- g$layout[idx, c("l", "r")] - 1; idx <- grep("strip-top", g$layout$name); g$layout[idx, c("t", "b")] <- g$layout[idx, c("t", "b")] + 1 ` – user20650 Apr 20 '16 at 12:56
  • If you're rubbish in it then what am I :D Thank you very much for all your help! I had a look at your label removing solution for ggpairs, too and liked it a lot. I'll post one very last question to that post, too :) – Marie-Louise Apr 20 '16 at 13:13
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.

Example

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

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

Results

The code would produce the desired chart without the diagonal plot: No diagonal

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 1
    in addition, also adding `upper = "blank"` only leaves the scatterplots – erc Apr 20 '16 at 11:17
  • @beetroot That's correct, as `upper` and `lower` are lists; it may be worth mentioning that other fancy stuff can be easily achieved with `pairs` when the one can pass custom functions to panels to get neatly formatted coefficients or custom notes. I often use pairs to address custom requests, like flagging coefficients of certain value. – Konrad Apr 20 '16 at 11:21
  • Is there a way to do the same with ggscatmat and not ggpairs? upper="blank" or upper=NULL don't work for it. And as I edited in my question just a sec before seeing your answer I now have two superfluous labels – Marie-Louise Apr 20 '16 at 11:22