Creating umbrella titles for aligned graphs in R

2019-08-23 02:15发布

问题:

I have succeeded in creating and aligning three scatter-plots in R, using the following code:

par(mfrow = c(3,1))

plot(CGP.GOSL ~ FPT.MAF.GOSL, data = all.locs, main = "A. Place I")
  abline(h=c(0.5))
  abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)
plot(CGP.IRE ~ FPT.MAF.IRE, data = all.locs, main = "B. Place II")
  abline(h=c(0.5))
  abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)
plot(CGP.BAR ~ FPT.MAF.BAR, data = all.locs, main = "C. Place III")
  abline(h=c(0.5))
  abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)

What I would like to do now is save space by having a single Axis label for the x and y axis. I have tried experimenting with the par() function, inserting x and ylab functions, but it seems that as these are not graphical parameters is will not accept them. I suspect the problem lies in where I place this information in the code, as using the xlab and ylab seems to make sense, and I can write x and ylab = "" in the individual plot codes.

I am also struggling to change the position of the main titles so that the appear on the left, to remove the values from the x-axis so that they only show at the bottom of the whole figure, and to arrange the figure so that there is less space.

This figure shows the current layout and the layout I want to achieve:

I am sorry to post so many questions at once. I am very new to R and programming am still finding the helpfiles a bit daunting, although I am getting there. Some suggestions on functions, where to put them and how to use them to achieve some of these aims would be great.

回答1:

The documentation can be a bit challenging at times. Here's a skeleton for what I think you're looking for:

# 3 rows
par(mfrow=c(3,1))

# tighter margins
par(mar = c(0, 0, 0, 0), oma = c(4, 4, 0.5, 0.5))

# need some data
data(cars)

# 3 plots, no axis junk
plot(cars, ann=FALSE)
plot(cars, ann=FALSE)
plot(cars, ann=FALSE)

# outer labels
mtext("x axis", side = 1, outer = TRUE, cex = 0.7, line = 2.2)
mtext("y axis", side = 2, outer = TRUE, cex = 0.7, line = 2.2)



回答2:

This answer is based on hrbrmstr's answer, but the result is closer to the requested layout:

# 3 rows
par(mfrow=c(3,1))

# Adjust margins. Each vector element refers to one side of the plot; 
# the order is c(bottom, left, top, right). (See ?par)
par(mar = c(2.5, 4.1, 1, 2.1), oma = c(3, 3, 2, 0))

# need some data
data(cars)


# 3 plots. On the first two: Suppress axis labels (ann = FALSE) and 
# the x axis (xaxt = "n"), then add the ticks using axis() and the
#  title using mtext(). On the last one, do not suppress x axis.
# Note that repeating arguments could be set globally using par().

plot(cars,  ann = FALSE, xaxt = "n")
axis(side = 1, labels = FALSE)
mtext(text = "A. Place I", side = 3, at = par("usr")[1], line = 1)

plot(cars, ann=FALSE, xaxt = "n")
axis(side = 1, labels = FALSE)
mtext(text = "B. Place II", side = 3, at = par("usr")[1], line = 1)

plot(cars, ann=FALSE)
mtext(text = "C. Place III", side = 3, at = par("usr")[1], line = 1)

# outer labels
mtext("X Axis label", side = 1, outer = TRUE)
mtext("Y Axis label", side = 2, outer = TRUE)