rplotpar

multi panel plot show x-axis labels while minimizing white space base r


I used the answer from this question: Avoid wasting space when placing multiple aligned plots onto one page

par(mfrow = c(5, 2),     # 2x2 layout
    oma = c(2, 2, 0, 0), # two rows of text at the outer left and bottom margin
    mar = c(1,6,2,1), # space for one row of text at ticks and to separate plots
    mgp = c(2, 1, 0),    # axis label at 2 rows distance, tick labels at 1 row
    xpd = NA)            # allow content to protrude into outer margin (and beyond)

and I get very close with the plot I have: enter image description here

But the x-axis labels are cut off in the final plot. I need them to be seen, and they need to stay rotated.

I tried

par(mar = c(1,6,2,1)+1)

before the final 2 plots for the panel but that changes the size of the actual plot instead of making the x-axis labels visible.

How can I make the x-axis labels visible?

If you can use the data from the linked example that would work fine.

set.seed(42)
catA <- factor(c("m100", "m500", "m1000", "m2000", "m3000", "m5000"))
catB <- factor(20:28)
samples <- 100
rsample <- function(v) v[ceiling(runif(samples, max=length(v)))]
Tab <- data.frame(catA = rsample(catA),
                  catB = rsample(catB),
                  valA = rnorm(samples, 150, 8),
                  valB = pmin(1,pmax(0,rnorm(samples, 0.5, 0.3))))
op <- par(mfrow = c(2,2),
          oma = c(5,4,0,0) + 0.1,
          mar = c(0,0,1,1) + 0.1)
for (i in 0:3) {
  x <- Tab[[1 + i %% 2]]
  plot(x, Tab[[3 + i %/% 2]], axes = FALSE)
  axis(side = 1,
       at=1:nlevels(x),
       labels = if (i %/% 2 == 1) levels(x) else FALSE)
  axis(side = 2, labels = (i %% 2 == 0))
  box(which = "plot", bty = "l")
}
title(xlab = "Some Categories",
      ylab = "Some Values",
      outer = TRUE, line = 3)
par(op)

Solution

  • Labels get automatically hidden according to the margins size.You are probably using the RStudio "Plots" tab, and if you resize the window, more labels show up. Better use another device, e.g. pdf or png device, where you may define a fixed size and the output is always the same.

    You could use a case handling via modulo for the entire axes, not just the labels. Further you could define the las parameters which rotates the tick labels, also using modulo, yielding 1 (always horozontal) or 2 (always perpendicular) depending on case (here long or short labels). Slightly expand second oma to show y axis label.

    png('plot1.png', width=600, height=600)  ## open device
    
    op <- par(mfrow=c(2, 2), oma=c(6, 6, 0, 0) + 0.1, mar=c(0, 0, 1, 1) + 0.1)
    
    for (i in 0:3) {
      x <- Tab[[1 + i %% 2]]
      plot(x, Tab[[3 + i %/% 2]], axes=FALSE)
      if (i %/% 2 == 1) {
        axis(side=1, at=1:nlevels(x), labels=levels(x), las=(1 - i %% 2) + 1)
      }
      if (i %% 2 == 0) {
        axis(side=2, labels=TRUE, las=2)
      }
      box()
    }
    title(xlab="Some Categories", ylab="Some Values", outer=TRUE, line=4)
    
    par(op)
    
    dev.off()  ## close device (plot is saved in wd)
    

    enter image description here

    I assumed you only wanted to show axis ticks and labels at the outer margins, otherwise, please comment.


    Data:

    Tab <- structure(list(catA = structure(c(6L, 6L, 5L, 4L, 3L, 3L, 4L, 
    1L, 3L, 4L, 2L, 4L, 6L, 5L, 2L, 6L, 6L, 1L, 2L, 3L, 6L, 1L, 6L, 
    6L, 1L, 3L, 2L, 6L, 2L, 6L, 4L, 4L, 2L, 4L, 1L, 4L, 1L, 5L, 6L, 
    3L, 2L, 2L, 1L, 6L, 2L, 6L, 6L, 3L, 6L, 3L, 2L, 2L, 2L, 4L, 1L, 
    4L, 4L, 5L, 5L, 3L, 4L, 6L, 4L, 3L, 6L, 5L, 5L, 4L, 4L, 5L, 1L, 
    1L, 5L, 2L, 5L, 4L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 4L, 3L, 5L, 
    1L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 4L, 5L, 3L, 4L, 3L), levels = c("m100", 
    "m1000", "m2000", "m3000", "m500", "m5000"), class = "factor"), 
        catB = structure(c(6L, 2L, 2L, 4L, 9L, 9L, 7L, 7L, 5L, 1L, 
        6L, 8L, 7L, 5L, 5L, 5L, 1L, 4L, 6L, 8L, 4L, 4L, 6L, 6L, 7L, 
        4L, 9L, 9L, 3L, 7L, 9L, 6L, 6L, 9L, 8L, 6L, 8L, 2L, 7L, 6L, 
        2L, 1L, 5L, 8L, 7L, 8L, 2L, 9L, 3L, 2L, 7L, 3L, 8L, 4L, 7L, 
        7L, 2L, 1L, 2L, 7L, 9L, 5L, 6L, 2L, 5L, 2L, 5L, 3L, 2L, 2L, 
        7L, 4L, 4L, 5L, 4L, 2L, 8L, 6L, 8L, 7L, 9L, 8L, 3L, 3L, 7L, 
        7L, 9L, 8L, 2L, 3L, 2L, 8L, 2L, 2L, 1L, 1L, 5L, 2L, 7L, 7L
        ), levels = c("20", "21", "22", "23", "24", "25", "26", "27", 
        "28"), class = "factor"), valA = c(159.607723004788, 158.358008697342, 
        141.974330825281, 164.787855213382, 144.665812729937, 150.844110499649, 
        146.621952945049, 149.02119862436, 151.505544276012, 150.953287663976, 
        149.799259593061, 150.864581823536, 146.116518113227, 145.966262954497, 
        136.711207360681, 146.941330185009, 145.898797936978, 171.615128002758, 
        139.103070150482, 151.098049748469, 138.05099946147, 138.236514068506, 
        150.997619089576, 142.026886920928, 149.985419085562, 146.573928948593, 
        145.090627148404, 133.802577236647, 140.20201639712, 151.436131528943, 
        154.540964755388, 146.056981171572, 150.000503072523, 158.98311714704, 
        161.51884594381, 141.223089852753, 149.061443517999, 159.611987207358, 
        146.24216335547, 149.580244120488, 149.311141614103, 142.898567856749, 
        146.442527960922, 149.764440967294, 146.689049207537, 158.907088186946, 
        146.152057266768, 146.534647739194, 155.574900612417, 141.549052694633, 
        149.67441219879, 137.587641421219, 159.337356393885, 147.810834389007, 
        146.257237402622, 140.09398137611, 149.937903729781, 143.597742576387, 
        145.732061360397, 160.301401964677, 148.595793038063, 141.425740926795, 
        151.305655059739, 147.098092674976, 154.720108383899, 161.459375421848, 
        142.058459911124, 153.637202380642, 150.679184469428, 157.164524658116, 
        148.16177488843, 156.692952547685, 136.039553109306, 163.515671370507, 
        156.918223828149, 148.793792088914, 138.407942958887, 155.144069600336, 
        153.865550910518, 149.949154988629, 151.211647142899, 145.327128237202, 
        152.950453861042, 152.357234717756, 147.765925013259, 139.310106760855, 
        155.60599054752, 154.433572978192, 143.309547257589, 137.24329470395, 
        151.639668644701, 147.239296176217, 152.020893626916, 139.647980276124, 
        142.326636444957, 158.686198829439, 153.230199237726, 154.691900293754, 
        164.521827569232, 151.030571428819), valB = c(0, 0.600133159230071, 
        0.851397538207638, 1, 0.0869415205278437, 0.154743330311868, 
        0.288253581571964, 0.183783265376843, 0.306276883057253, 
        0.444386609697049, 0.139633384778004, 1, 0.532332423465664, 
        0.474767569848326, 0.648685892481378, 0.51122455583539, 0.460373588913227, 
        0.943036227065629, 0.434890936972369, 0.114919338772333, 
        0.615700367133021, 0.394546137941272, 0.343461171993119, 
        0.179560639793849, 0.628509770980008, 0.447794529671902, 
        0.654700318594409, 0.429690416808224, 0.302448972253469, 
        0.875070981223615, 0.418470885466581, 0.784385598762559, 
        0.139525270967319, 0.360165171087349, 0.419194581454046, 
        0.382710377560742, 0.904612103597514, 0.493170589610476, 
        0.573267755331035, 0.217288487640823, 0.281234817047128, 
        0.799420672566455, 0.877544499378668, 0.874659106643029, 
        0.085808885142401, 1, 0.805061848939862, 0.491984760758523, 
        0.711082333639478, 0.208584431254441, 0.171153127532343, 
        0.514715135281279, 0.140451243032666, 0.557005699567924, 
        0.8893117698814, 0.189837883110575, 0.278467773736081, 0.513969181835259, 
        0.194721164052168, 0.385014812034117, 0.761826623501419, 
        0.790863504191035, 0.615153999508128, 0, 0.48380097895215, 
        0.819431964301485, 0.743958511223481, 0.442755057768715, 
        0, 0.518289991640335, 0.672125509245073, 0.51374107391753, 
        0.547223762052763, 0.629469611860631, 0.381035079184936, 
        0.892993467725465, 0.641118019962183, 0.12719891882762, 0.91447263691566, 
        0.861337681102947, 0.747222189104564, 0.00121117934432169, 
        0.329208096918356, 0.690654145186858, 0.513116602273357, 
        0.604403691098392, 1, 0.254485902680099, 0, 0.582108581729124, 
        0.293720947626976, 0.63381231588856, 0.256284582862821, 1, 
        0.462888208526771, 0.356799348190984, 0.450121552554018, 
        0.758769015087929, 0.529202145560846, 0.0123149782366136)), class = "data.frame", row.names = c(NA, 
    -100L))