Setting the ggpairs
aspect ratio causes axis tick labels to overlap and become unreadable. How can I fix this?
library(GGally)
ggpairs(mtcars[, 1:3]) + theme(aspect.ratio=1)
1
As zx8754 says, this is an open issue with GGally. The ggpairs
function works in a somewhat non-standard way within the ggplot ecosystem, drawing axes back into the grob table after it is already built.
There is no easy solution, but there are workarounds.
One is to specify the size of the rows and columns of the gtable which actually contain the plot panels. This will effectively fix the size of your plot though, so you need to make sure the dimensions are appropriate to your output medium.
First create the plot, convert it to a grob table and store it:
p <- ggpairs(mtcars[, 1:3]) |> ggmatrix_gtable()
Now fix the size of your panels:
panel_size <- grid::unit(5, "cm")
p$widths[grid::unitType(p$widths) == "null"] <- panel_size
p$heights[grid::unitType(p$heights) == "null"] <- panel_size
Finally, you can draw your grob table using grid
functions:
grid::grid.newpage()
grid::grid.draw(p)
Each panel will be exactly 5cm x 5cm in your plot window. You can change the size to suit your needs.