【问题标题】:How to plot legend in place of a plot multiple plots per window in R如何在 R 中绘制图例而不是在每个窗口中绘制多个图
【发布时间】:2021-09-23 23:46:36
【问题描述】:

我想为我的三个地块放置一个共同的图例在第四个地块的位置(以黄色突出显示)。我该怎么做?

重现情节的代码

par(mfrow = c(2, 2))

x <- rep(1:9, 3)
y <- sample(1:9, 27, replace = T)
group <- rep(1:3, each = 9)

for(i in 1:3){
  plot(x = x, y = y, type = "n")
  for(j in 1:3){
    print(c("red", "blue", "green")[group])

    lines(x = x[group == j],
          y = y[group == j],
          col = c("red", "blue", "green")[j])
  }
}

【问题讨论】:

    标签: r data-visualization legend


    【解决方案1】:

    您可以在最后一个正方形中绘制一个“空白”图,然后在顶部绘制图例

    for(i in 1:3){
      plot(x = x, y = y, type = "n")
      for(j in 1:3){
        print(c("red", "blue", "green")[group])
        
        lines(x = x[group == j],
              y = y[group == j],
              col = c("red", "blue", "green")[j])
      }
    }
    plot(0, 0, type = 'l', bty = 'n', xaxt = 'n', yaxt = 'n', xlab="", ylab="")
    legend('center',
      legend = c("Red", "Blue", "Green"), 
      col = c("red","blue", "green"), 
      lwd=1, xpd = TRUE, cex = 1, seg.len=1, bty = 'n')
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多