thiagoveloso
thiagoveloso

Reputation: 2763

R - combine ten plots in matrix layout

I am trying to combine ten plots using a matrix layout. I am almost there, but there is a problem I need to fix. Please take a look at what I have so far:

# Reproduce dataset
obs <- c(8.12903225806452, 4.65483870967742, 4.00967741935484, 2.82903225806452, 
1.02258064516129, 4.94838709677419, 2.03548387096774, 4.56129032258065, 
6.66451612903226, 10.4129032258065, 3.7741935483871, 9.93870967741936)

uncorrected <- structure(list(CanESM2 = c(3.71099209785461, 6.68828105926514, 
4.2675461769104, 4.29092979431152, 6.47999143600464, 6.30958032608032, 
3.28256869316101, 1.06898427009583, 4.01592206954956, 7.88787031173706, 
10.8663206100464, 9.14756298065186), GFDL.ESM2M = c(4.56643295288086, 
8.36136913299561, 4.6934700012207, 4.62750291824341, 6.34791326522827, 
0.916408598423004, 0.111764870584011, 0.570569038391113, 3.05491662025452, 
3.29908037185669, 5.98263263702393, 7.52161026000977), MRI.CGCM3 = c(3.94400858879089, 
2.48861312866211, 0.663281202316284, 0.422377318143845, 1.49548053741455, 
1.8869035243988, 5.35201597213745, 3.94722390174866, 5.0041446685791, 
6.11909627914429, 6.37435054779053, 5.9401068687439), NorESM1.M = c(8.83542060852051, 
8.36209106445312, 3.22844076156616, 2.86131143569946, 2.05858564376831, 
2.39689040184021, 1.06175291538239, 1.03921580314636, 2.49208211898804, 
6.83605766296387, 7.95251750946045, 4.8852391242981), inmcm4 = c(5.88600730895996, 
8.03514385223389, 3.86212635040283, 1.51728367805481, 3.46821308135986, 
1.30993270874023, 0.497318655252457, 0.589299857616425, 1.63358855247498, 
3.61927103996277, 4.99840354919434, 10.5596942901611)), .Names = c("CanESM2", 
"GFDL.ESM2M", "MRI.CGCM3", "NorESM1.M", "inmcm4"), class = "data.frame", row.names = c(NA, 
-12L))

corrected <- structure(list(CanESM2 = c(4.69295692443848, 6.2362003326416, 
4.48602199554443, 4.21368360519409, 6.02497625350952, 5.94498348236084, 
3.92529225349426, 1.07059073448181, 3.92306709289551, 8.00215721130371, 
10.9688482284546, 9.95723056793213), GFDL.ESM2M = c(6.74847173690796, 
10.7781000137329, 6.89084196090698, 7.5780816078186, 8.96726703643799, 
3.57111501693726, 1.20403492450714, 2.76221370697021, 4.98387718200684, 
5.27927255630493, 8.87845706939697, 9.95864582061768), MRI.CGCM3 = c(5.45173072814941, 
3.37272930145264, 2.20000958442688, 1.12101686000824, 2.67246603965759, 
3.10278224945068, 6.02392053604126, 4.70185708999634, 5.86025190353394, 
6.41299057006836, 7.1956262588501, 7.59840393066406), NorESM1.M = c(9.49540519714355, 
9.88559532165527, 4.86537599563599, 4.56189870834351, 3.85974431037903, 
4.07706260681152, 2.55634951591492, 2.63215637207031, 4.21092081069946, 
7.66422271728516, 8.75070095062256, 5.95497798919678), inmcm4 = c(8.21576976776123, 
11.1972188949585, 6.08181619644165, 3.58428621292114, 5.543297290802, 
3.46666693687439, 1.88991332054138, 1.94812619686127, 3.71879911422729, 
5.70409107208252, 6.85269260406494, 11.8774194717407)), .Names = c("CanESM2", 
"GFDL.ESM2M", "MRI.CGCM3", "NorESM1.M", "inmcm4"), class = "data.frame", row.names = c(NA, 
-12L))

# Create layout
m <- cbind(c(1,5,9), c(2,6,10), c(3,7,9), c(4,8,10))
layout(m)

# Plot
for (i in 1:length(names(uncorrected))) {
  # Plot uncorrected vs obs
  qqplot(as.vector(obs), uncorrected[,i], xlim=c(0,14), ylim=c(0,14),
         xlab=expression("Obs. precip." ~ (mm ~ day^{-1})),
         ylab=expression("Mod. precip." ~ (mm ~ day^{-1})))
  abline(0, 1)
  # Then plot corrected vs obs
  qqplot(as.vector(obs), corrected[,i], xlim=c(0,14), ylim=c(0,14),
         xlab=expression("Obs. precip." ~ (mm ~ day^{-1})),
         ylab=expression("Mod. precip." ~ (mm ~ day^{-1})))
  abline(0, 1)
}

This is almost what I need. However, the last subplot is stretched to the length of the remaining matrix row. Instead, I'd like to have the two plots in the last row aligned in the center, in the same length and width as the others.

Is this possible?

Upvotes: 1

Views: 431

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73315

The problem is with your layout matrix m:

#     [,1] [,2] [,3] [,4]
#[1,]    1    2    3    4
#[2,]    5    6    7    8
#[3,]    9   10    9   10

In the final row, you have 9, 10, 9, 10 rather than 9, 9, 10, 10.

Use

m <- cbind(c(1,5,9), c(2,6,9), c(3,7,10), c(4,8,10))

instead.

enter image description here

I'd like to have the two plots in the last row aligned in the center, in the same length and width as the others.

By retaining the same subplot size, we have to allocate 12 slots but use only 10 of them. layout plot starts placing plots from index 1, then place the rest in order. So, we can do:

m <- cbind(c(1,5,11), c(2,6,9), c(3,7,10), c(4,8,12))

#     [,1] [,2] [,3] [,4]
#[1,]    1    2    3    4
#[2,]    5    6    7    8
#[3,]   11    9   10   12

enter image description here

Upvotes: 4

Related Questions