I'm using the code below to generate the following chart.
# Setup
data(airquality)
# Device start
png(filename = "example.png", units = "cm", width = 20, height = 14, res = 300)
# Define chart
pairs.chrt <- ggpairs(airquality,
lower = list(continuous = "smooth"),
diag = list(continuous = "blank"),
upper = list(continuous = "blank")) +
theme(legend.position = "none",
panel.grid.major = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_text(angle = 180, vjust = 1, color = "black"),
panel.border = element_rect(fill = NA))
# Device off and print
print(pairs.chrt)
dev.off()
I'm currently trying to modify the display of the axis titles. In particular, I would like for the axis titles to be:
- Placed at a further distance from axis labels
- Placed at an angle
As an example, I would like to obtain axis titles similar to the ones pictured below (I'm interested in axis labels only, not in rest of the chart): Taken from : Geovisualist
I' tried adjusting my syntax changing the axis.title.x
to different values but it does not yield the desired results. For instance running the code with angle = 45
.
axis.title.x = element_text(angle = 45, vjust = 1, color = "black"),
panel.border = element_rect(fill = NA))
returns the same chart. I was able to control the axis labels by changing the axis.text.x
for instance but I can't find the answer how to control the axis titles in this plot. Any help will be much appreciated.
Short answer: There doesn't seem to be an elegant or easy way to do it, but here's a workaround.
I dug into the
ggpairs
source code (in the GGally package source available from CRAN) to see how the variable labels are actually drawn. The relevant function inggpairs.R
isprint.ggpairs
. It turns out the variable labels aren't part of theggplot
objects in each cell of the plot matrix -- i.e. they're not axis titles, which is why they aren't affected by usingtheme(axis.title.x = element_text(angle = 45)
or similar.Rather, they seem to be drawn as text annotations using
grid.text
(in package'grid'
).grid.text
takes arguments includingx, y, hjust, vjust, rot
(whererot
is angle of rotation), as well as font size, font family, etc. usinggpar
(see?grid.text
), but it looks like there is currently no way to pass in different values of those parameters toprint.ggpairs
-- they're fixed at default values.You can work around it by leaving your variable labels blank to begin with, and then adding them on later with customized placement, rotation, and styling, using a modification of the relevant part of the
print.ggpairs
code. I came up with the following modification. (Incidentally, because the originalGGally
source code was released under a GPL-3 license, so is this modification.)And here's an example of calling that function:
(This makes some very ugly labels -- for the purposes of demonstration only!)
I didn't tinker with placing the labels somewhere other than the left and bottom -- as in your Geovisualist example -- but I think you'd do it by changing the arguments to
vplayout
in the "Left Side" and "Bottom Side" pieces of code incustomize.labels
. Thex
andy
coordinates ingrid.text
are defined relative to a viewport, which divides the display area into a grid inThe call to
vplayout
specifies which cell of the grid is being used to position each label.My answer won't fix the diagonal label issue but it will fix the overlay one.
I had this issue with the report I am currently writing, where the axis titles were always over the axes, especially in ggpairs. I used a combination of adjusting the out.height/out.width in conjunction with fig.height/fig.width. Separately the problem was not fixed, but together it was. fig.height/fig.width took the labels away from the axis but made them too small to read, and out.height/out.width just made the plot bigger with the problem unchanged. The below gave me the results shown:
before:plot with issues
after:
Caveat: not a complete answer but perhaps suggests a way to approach it. You can do this by editing the
grid
objects.