Home > other >  base R: how to keep axis box in each plot of a 2 x 2 figure
base R: how to keep axis box in each plot of a 2 x 2 figure

Time:02-01

I am making a 4-panel figure where each column represents a different variable and each row represents a different location. When I add the first two plots (i.e., the top row) there is a box surrounding each plot. However, when I add the bottom two figures (i.e., the bottom row) the box around the top row plots disappears. Any idea how to make sure there is a box around each plot?

Example

Make the two figures of the top row first

library(biwavelet)

par(mfrow = c(2,2), xpd=FALSE)
par(mar = c(1,1,1.1,1.1)   0.1,
    oma = c(3,3,0.5,0.5)   0.1)

dat <- as.data.frame(matrix(nrow = 500, ncol = 2))

dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])

# top-left figure
plot(wt(dat),
     xaxt = 'n',
     xlab = "",
     cex.axis = 1.5,
     cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')

# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
     xaxt = 'n',
     col.axis = 'NA',
     xlab = "",
     ylab = ""); par(xpd = NA)
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1); par(xpd = NA)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')

enter image description here

Now add bottom two plots and watch the black box around the top two plots disappear

# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
     cex.axis = 1.5,
     xlab = "",
     ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')

# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
     col.axis = 'NA',
     ylab = "",
     xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5); par(xpd = NA)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5)
box(lty = "solid", col = 'black')

title(xlab = "Time (hours)",
      ylab = "Period",
      outer = TRUE,
      line = 1.5,
      cex.lab = 1.5)

enter image description here

I have tried using box(lty = "solid", col = 'black'), which has not worked. I have also tried using fg = 'black' and bty = "o" inside of plot(), which has not worked.

How can I keep the black box around the top two plots?

CodePudding user response:

With par(xpd=NA) before your text() commands you are doing the xpd=NA setting globally, hence influencing all subsequent plotting, and your bottom row graphs are somehow overdrawing the top row ones. Just move the xpd=NA inside your text() functions (e.g. text('Location 1', ..., xpd=NA)) and everything should work as expected:

library(biwavelet)

par(mfrow = c(2,2), xpd=F)
par(mar = c(1,1,1.1,1.1)   0.1,
    oma = c(3,3,0.5,0.5)   0.1)

dat <- as.data.frame(matrix(nrow = 500, ncol = 2))

dat[1] <- seq(1,500)
dat[2] <- sin(dat[1])

# top-left figure
plot(wt(dat),
     xaxt = 'n',
     xlab = "",
     cex.axis = 1.5,
     cex.lab = 1.5)
axis(1, at = seq(0, 500, by = 100), cex.axis = 1.5, col.axis = 'NA')
mtext("Variable 1", side = 3, cex = 1.5, line = 0.1)
box(lty = "solid", col = 'black')

# top-right figure
dat[2] <- sin(dat[1]*.5)
plot(wt(dat),
     xaxt = 'n',
     col.axis = 'NA',
     xlab = "",
     ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 0.1, col.axis = 'NA')
mtext("Variable 2", side = 3, cex = 1.5, line = 0.1)
text("Location 1", x = 520, y = 4.1, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')

# bottom-left figure
dat[2] <- sin(dat[1]*.25)
plot(wt(dat),
     cex.axis = 1.5,
     xlab = "",
     ylab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
box(lty = "solid", col = 'black')

# bottom-right figure
dat[2] <- sin(dat[1]*.125)
plot(wt(dat),
     col.axis = 'NA',
     ylab = "",
     xlab = "")
axis(1, at = seq(100, 500, by = 100), cex.axis = 1.5)
text("Location 2", x = 520, y = 4.5, srt = 270, cex = 1.5, xpd=NA)
box(lty = "solid", col = 'black')

title(xlab = "Time (hours)",
      ylab = "Period",
      outer = TRUE,
      line = 1.5,
      cex.lab = 1.5)

plots with boxes

  • Related