如何在R中的多图块面板上的图块之间或之间进行注释 [英] How to annotate across or between plots in multi-plot panels in R
问题描述
如果我在单个面板中有多个图块,我希望能够将文本注释跨越多个绘图区域或位于绘图区域之间。我确信必须有办法做到这一点,但我需要一些帮助来弄清楚如何。
这里有一些假数据在一个面板上创建四个图:
If I have several plots in a single panel, I would like to be able to put text annotations that go across more than one plot area or that go in between plot areas. I'm sure there must be a way to do this, but I need some help figuring out how.
Here's some fake data to create four plots on one panel:
x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
x4 = rnorm(100)
par(mfrow=c(2,2))
hist(x1, xlab="", main="Group A")
hist(x2, xlab="", main="Group B")
hist(x3, xlab="", main="")
hist(x4, xlab="", main="")
这会创建下面的多图板,除了我还以红色添加了文本。我通过在之后注释PDF文件添加了红色文本,我在R中创建了图形面板,然后将其保存为PDF。我想学习的是如何在R中以编程方式注释图形面板,而不是必须保存并注释保存的文件。
通常,我只需使用<$ c $添加注释c> text()或 mtext()
。但是,只有当你注释一张图时才有效。有没有办法添加注释相对于4个图的整个面板的坐标?我现在正在寻找基础图形解决方案,但是如果有人知道如何在ggplot2或lattice中做类似的事情,我也会对此感兴趣。
This creates the multi-plot panel below, except that I've also added text in red. I added the red text by annotating the PDF file after I created the graph panel in R and then saved it as a PDF. What I'd like to learn is how to annotate the graph panel programmatically within R, rather than having to save it and annotate the saved file.
Normally, I would just add an annotation using text()
or mtext()
. But that only works if you're annotating a single graph. Is there a way to add annotations relative to the coordinates of the entire panel of 4 graphs? I'm looking for a solution in base graphics right now, but if anyone knows how to do something similar in ggplot2 or lattice, I'd be interested in seeing that as well.
推荐答案
如果您真的想更好地控制这些布局问题,可以使用恰当命名的 layout
。
If you truly want finer control over these kinds of layout issues, you can use the aptly named layout
.
m <- matrix(c(1,2,3,3,4,5,6,6),ncol = 2,byrow = TRUE)
layout(m,widths = c(0.5,0.5),heights = c(0.45,0.05,0.45,0.05))
par(mar = c(2,4,4,2) + 0.1)
hist(x1, xlab="", main="Group A")
hist(x2, xlab="", main="Group B")
par(mar = c(0,0,0,0))
plot(1,1,type = "n",frame.plot = FALSE,axes = FALSE)
u <- par("usr")
text(1,u[4],labels = "Here",col = "red",pos = 1)
par(mar = c(2,4,2,2) + 0.1)
hist(x3, xlab="", main="")
hist(x4, xlab="", main="")
par(mar = c(0,0,0,0))
plot(1,1,type = "n",frame.plot = FALSE,axes = FALSE)
u <- par("usr")
text(1,u[4],labels = "Here",col = "red",pos = 1)
这篇关于如何在R中的多图块面板上的图块之间或之间进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!