R plot:显示多个绘图时在设备区域中间插入文本 [英] R plot: Insert text in the middle of device region when showing multiple plots

查看:50
本文介绍了R plot:显示多个绘图时在设备区域中间插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 4x3 格式的图,但是只显示了 10 个图.

I have a plot with a 4x3 format, however only 10 plots are shown.

par(mfrow=c(4,3)) #Grid layout

现在我想为所有人插入一个图例!在完整设备区域的中间和最右上角绘制另一个文本以获取一些信息.但是现在使用此代码,图例位于最后一个(!)小图的中间,而文本位于最后一个图的最右上角.如何将它们都移动到我的设备中间?

Now I want to insert one legend for all! plots in the middle of the complete device region and at the far right corner another text for some info. However right now with this code, the legend is in the middle of the last(!) small plot and the text at the far right corner of the last plot. How can I move them both into the middle of my device?

  #Legend
  par(xpd=NA)
  legend(legend="Legend", col=hblue,lty=1, lwd=2, bty="n", text.col="black", ncol=1, "bottom", 
         inset = c(0.0, -.1),cex=0.65) 

  #Inserting the source of data
  text_note=paste("Source:")
  mtext(text_note ,cex=0.4,col=hgrey,side = 1, line = 4, outer = FALSE,padj=0,adj=1)

推荐答案

使用 par(mfrow) 您正在为每个绘图分割设备区域,因此很难到达外部区域您当前所在的位置,您需要按顺序填充绘图区域.这是使用 layout 和外边距的策略.

With using par(mfrow) you are slicing up the device region for each plot so it can be difficult to get to regions outside of where you currently are and you need to fill the plot regions in order. Here's a strategy using layout and outer margins.

首先,我使用 layout() 来指定我想要创建绘图的顺序,我将中心正方形留到最后,这样我就可以最后创建图例.然后我创建一个空白的绘图窗口并只添加图例.

First, I use layout() to specify the order i want to create the plots, I leave the center square for the end so i can create the legend last. Then i create a blank plot window and add just the legend.

为了获得浮动文本,我使用了外边距区域.我使用 par(oma=c(2,0,0,0)) 在绘图区域的底部请求一些额外的空间,然后我使用 mtext()将文本添加到任何单个绘图之外的外边缘区域.这是代码.

To get the floating text, I use the outer margin region. I use par(oma=c(2,0,0,0)) to request some extra room at the bottom of the plotting area, then I use mtext() to add the text to that outer margin region outside of any individual plot. Here's the code.

par(oma=c(2,0,0,0))
layout(matrix(c(1,2,3,4,9,5,6,7,8), byrow=T, ncol=3))

for(i in 1:8) {
    plot(1:10, runif(1:10), main=paste("plot", i))
}

plot.new()
plot.window(0:1, 0:1)
legend("center","center", c("Apples","Oranges"), col=c("red","orange"), pch=20)

mtext("source", side=1, outer=T, adj=.9)

这是输出.

如果您只想要带有图例的中心文本,您可以忘记外边距和 mtext 并使用

If you just wanted the text in the center with the legend, you can forget about the outer margins and the mtext and just use

text(.5,0, "Source")

在调用 legend() 之后.这将在中间图的底部中心添加文本.

right after the call to legend(). That will add the text at the bottom center of the middle plot.

这篇关于R plot:显示多个绘图时在设备区域中间插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆