DJ-AFC
DJ-AFC

Reputation: 569

Aligning awkward plot and axis titles

I have created the following plot showing changes in temperatures (left column) and changes in precipitation (right column) from 1979-2015 for each of the 4 seasons (each row):

enter image description here

I now wish to add a common y-axis label to the 4 plots on the left side of the plot called "Temperatures (°C)" and the same on the right side of the plot called "Precipitation (mm)". I also wish to have a centre-aligned title for each row of plots with the names of the seasons. The output should look like:

enter image description here

I have looked at various examples of plotting with invisible plots to create axis titles etc. but cannot adapt any of this to my situation. Any advice much appreciated.

Upvotes: 1

Views: 251

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73315

As I said in my comment 5 hours ago, there is a way to settle all positioning, by using mtext(); it is just that I don't know whether this is the best solution. However, there has been 5 hours and no answer arrives, so I decide to post mine.

There is no difficulty in placing "Temperatures", "Precipitations" and "Winter", but it requires some tuning on placing "Spring", "Summer" and "Autumn". The variable offset in the following template code controls such tuning. offset = -14.5 is near-optimal, when I produce the figure on:

jpeg(file = "template.jpeg", width = 600, height = 600, quality = 100)
## template code
dev.off()

Depending on the size of your plot, you need to adjust / reset offset by your needs.


Template Code

## set plot layout, inner margin and outer margin
par(mfrow=c(4,2), mar = c(1.5,2.5,1.5,2.5), oma = c(3,4,3,4))

## plot 1
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 2
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 3
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 4
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 5
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 6
plot(1:5, ann = FALSE, xaxt = "n")
axis(4, at = axTicks(4))
axis(1, at = axTicks(1), labels= NA)

## plot 7
plot(1:5, ann = FALSE)
axis(4, at = axTicks(4))

## plot 8
plot(1:5, ann = FALSE)
axis(4, at = axTicks(4))

## write text on outer margins
mtext("Temperatures (°C)", 2, outer = TRUE, line = 2, font = 2)
mtext("Precipitation (mm)", 4, outer = TRUE, line = 2, font = 2)
mtext("Winter", 3, outer = TRUE, line = 0, font = 2)

## needs tuning on `offset`
offset <- -14.5
mtext("Spring", 3, outer = TRUE, line = offset, font = 2)
mtext("Summer", 3, outer = TRUE, line = 2 * offset, font = 2)
mtext("Autumn", 3, outer = TRUE, line = 3 * offset, font = 2)

enter image description here

Upvotes: 1

Related Questions