在分区绘图中添加常用图例

时间:2016-02-10 17:08:06

标签: r plot legend partition

我想在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")

1 个答案:

答案 0 :(得分:1)

使用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")

resulting plot