1

I want to create a correlation matrix with ggpairs. I only need the lower part. The upper and diagonal parts are not relevant. Furthermore, I want to keep the final plot simple and therefore remove the axis labels at the top left and bottom right, where there are no more plots anyway (see below). I succeed in doing this, but my approach leads to the x-axis labelling also disappearing afterwards and I do not know why and how I can restore it.

My question therefore: How can I restore the x-axis labelling or is there another approach how I can produce the desired plot?

library(tidyverse)
library(GGally)

# Function to customize single plots within ggpairs
lowerFn <- function(data, mapping, ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(alpha = 0.8, size = 1) +
    geom_smooth(color = "red", method = 'lm', linewidth = 0.8, se = FALSE,...)
  p
}

# Create ggpairs plot
ggtest <- ggpairs(mtcars,
               column = 4:7,
               lower = list(continuous = wrap(lowerFn)),
               switch = "both",
               upper = list(continuous = "blank"),
               diag = list(continuous = "blankDiag"),
)
ggtest

# Customize ggpairs plot
ggtest$yAxisLabels = ggtest$yAxisLabels[2:4]
ggtest$xAxisLabels = ggtest$xAxisLabels[1:3]
ggtest$plots = ggtest$plots[c(5:7,9:11,13:15)]
ggtest

# x-axis labelling is missing in the final ggpairs plot

1 Answer 1

1

The output of ggpairs isn't supposed to be wrangled like this, as there are several interdependent data members of the final object which are used in plot creation. What you are doing is fairly hacky.

However, if you want this particular hack to work, you need to also update the nrow and ncol members of ggtest

Original

ggtest

enter image description here

Hacked:

ggtest$yAxisLabels = ggtest$yAxisLabels[2:4]
ggtest$xAxisLabels = ggtest$xAxisLabels[1:3]
ggtest$plots = ggtest$plots[c(5:7, 9:11, 13:15)]
ggtest$nrow <- 3
ggtest$ncol <- 3
ggtest

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Allan Cameron for the quick reply. Exactly what I was looking for!
@ChrisAI you're welcome. You tend to get an answer quickly round here if you ask a clear question and provide a minimal reproducible example, as you have done. This is one of the best first questions I have seen on Stack Overflow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.