向分区图添加通用图例

Add a common legend to a partitioned plot

我想在 R 中制作一个具有共同图例的分区图。

这是一个示例数据:

set.seed(1)
replicates <- 4
df <- data.frame(y = rep(rnorm(100),replicates), x = rep(rnorm(100),replicates),
                 replicate = c(sapply(1:replicates,function(x) rep(x,100))))


par(mfcol = c(4,4),
    mar = c(3,3,0.5,0.5), oma = rep(1,4))
for(i in 1:replicates){
  for(j in 1:replicates){
    plot(df$x[which(df$replicate == i)], df$x[which(df$replicate == j)], xlab = "", ylab = "")
    title(xlab = paste("replicate", i, sep = " "), ylab = paste("replicate", j, sep = " "), line = 2)
  }
}

我想出现在底部的常见图例应该是:

legend(legend = "measurements", pch = 16, col = "gray")

使用layout:

par(mar = c(3,3,0.5,0.5), oma = rep(1,4))
layout(rbind(matrix(1:16, 4), rep(17, 4)), heights = c(rep(1, 4), 0.5))
for(i in 1:replicates){
  for(j in 1:replicates){
    plot(df$x[which(df$replicate == i)], df$x[which(df$replicate == j)], xlab = "", ylab = "")
    title(xlab = paste("replicate", i, sep = " "), ylab = paste("replicate", j, sep = " "), line = 2)
  }
}
par(mar = c(0,0,0,0))
plot.new()
legend(x = "center", legend = "measurements", pch = 16, col = "gray")