图标签:在图的同一位置添加文本,不管图大小如何
Figure labels: add text on graphs in the same location despite figure size
我想知道如何在 R 中添加一个字母来引用图形,而不总是更改 y 和 x 值。我希望文本始终位于同一位置(在同一基线上,并且在 x 轴上的相对距离相同。
set.seed(1234)
plot(2,3)
text(x = 1.5,y = 4, "A", font = 2 )
plot(rnorm(100))
text(x = 1.5,y = 2, "B", font = 2)
如您所见,字母 B 在 x 轴上的距离不一样,而且有点向下。当我在 RStudio 中调整图形 window 大小时,我希望它能自动调整。
这就是我过去解决该问题的方法(基于这些问题:, ):
line2user <- function(line, side) {
lh <- par('cin')[2] * par('cex') * par('lheight')
x_off <- diff(grconvertX(c(0, lh), 'inches', 'npc'))
y_off <- diff(grconvertY(c(0, lh), 'inches', 'npc'))
switch(side,
`1` = grconvertY(-line * y_off, 'npc', 'user'),
`2` = grconvertX(-line * x_off, 'npc', 'user'),
`3` = grconvertY(1 + line * y_off, 'npc', 'user'),
`4` = grconvertX(1 + line * x_off, 'npc', 'user'),
stop("Side must be 1, 2, 3, or 4", call.=FALSE))
}
addfiglab <- function(lab, xl = par()$mar[2], yl = par()$mar[3]) {
text(x = line2user(xl, 2), y = line2user(yl, 3),
lab, xpd = NA, font = 2, cex = 1.5, adj = c(0, 1))
}
par(mfrow = c(1, 2))
plot(0)
addfiglab("A")
plot(1000)
addfiglab("B")
您可以使用cowplot::plot_grid
函数:
https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
示例:
require(cowplot)
require(ggplot2)
set.seed(1234)
# Create plot "template" with some dummy data
gg <- ggplot(data = data.frame(1,2), aes(x = X, y = Y)) +
theme_light() +
theme(plot.margin = unit(c(10.5, 10.5, 5.5, 5.5), "points"))
# Create the actual plots
gg1 <- gg + geom_point(data = data.frame(X = 2, Y = 3))
gg2 <- gg + geom_point(data = data.frame(X = rnorm(100), Y = rnorm(100)))
plot_grid(gg1, gg2, labels = c("A", "B"))
另一种选择是使用 mtext
左对齐和 xpd=NA
:
set.seed(1234)
layout(mat=matrix(c(1,2,3,3,4,4,4,4), ncol=4))
plot(rnorm(100))
mtext(text=LETTERS[1], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[2], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[3], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[4], xpd=NA, side=3, adj=0, font=2)
您只需要使用 par("usr")
获取 y 轴限制,然后相应地设置文本:
set.seed(1234)
# Define the y value where the text is to be placed on the first graph
textY = 2
# Plot the graph, text and get the axis limits
plot(2,3)
text(x = 1.5,y = textY, "A", font = 2 )
yA = par("usr")
#Plot the second graph and get the axis limits
plot(rnorm(100))
yB = par("usr")
# Calculate the y value where to place the text on the second graph
new_yB = (textY-yA[3])/(yA[4]-yA[3])*(yB[4]-yB[3])+yB[3]
text(x = 1.5,y = new_yB, "B", font = 2)
par("usr")
将 return 包含 4 个数字的向量:xLow、xHigh、yLow、yHigh
我想知道如何在 R 中添加一个字母来引用图形,而不总是更改 y 和 x 值。我希望文本始终位于同一位置(在同一基线上,并且在 x 轴上的相对距离相同。
set.seed(1234)
plot(2,3)
text(x = 1.5,y = 4, "A", font = 2 )
plot(rnorm(100))
text(x = 1.5,y = 2, "B", font = 2)
如您所见,字母 B 在 x 轴上的距离不一样,而且有点向下。当我在 RStudio 中调整图形 window 大小时,我希望它能自动调整。
这就是我过去解决该问题的方法(基于这些问题:
line2user <- function(line, side) {
lh <- par('cin')[2] * par('cex') * par('lheight')
x_off <- diff(grconvertX(c(0, lh), 'inches', 'npc'))
y_off <- diff(grconvertY(c(0, lh), 'inches', 'npc'))
switch(side,
`1` = grconvertY(-line * y_off, 'npc', 'user'),
`2` = grconvertX(-line * x_off, 'npc', 'user'),
`3` = grconvertY(1 + line * y_off, 'npc', 'user'),
`4` = grconvertX(1 + line * x_off, 'npc', 'user'),
stop("Side must be 1, 2, 3, or 4", call.=FALSE))
}
addfiglab <- function(lab, xl = par()$mar[2], yl = par()$mar[3]) {
text(x = line2user(xl, 2), y = line2user(yl, 3),
lab, xpd = NA, font = 2, cex = 1.5, adj = c(0, 1))
}
par(mfrow = c(1, 2))
plot(0)
addfiglab("A")
plot(1000)
addfiglab("B")
您可以使用cowplot::plot_grid
函数:
https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
示例:
require(cowplot)
require(ggplot2)
set.seed(1234)
# Create plot "template" with some dummy data
gg <- ggplot(data = data.frame(1,2), aes(x = X, y = Y)) +
theme_light() +
theme(plot.margin = unit(c(10.5, 10.5, 5.5, 5.5), "points"))
# Create the actual plots
gg1 <- gg + geom_point(data = data.frame(X = 2, Y = 3))
gg2 <- gg + geom_point(data = data.frame(X = rnorm(100), Y = rnorm(100)))
plot_grid(gg1, gg2, labels = c("A", "B"))
另一种选择是使用 mtext
左对齐和 xpd=NA
:
set.seed(1234)
layout(mat=matrix(c(1,2,3,3,4,4,4,4), ncol=4))
plot(rnorm(100))
mtext(text=LETTERS[1], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[2], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[3], xpd=NA, side=3, adj=0, font=2)
plot(rnorm(100))
mtext(text=LETTERS[4], xpd=NA, side=3, adj=0, font=2)
您只需要使用 par("usr")
获取 y 轴限制,然后相应地设置文本:
set.seed(1234)
# Define the y value where the text is to be placed on the first graph
textY = 2
# Plot the graph, text and get the axis limits
plot(2,3)
text(x = 1.5,y = textY, "A", font = 2 )
yA = par("usr")
#Plot the second graph and get the axis limits
plot(rnorm(100))
yB = par("usr")
# Calculate the y value where to place the text on the second graph
new_yB = (textY-yA[3])/(yA[4]-yA[3])*(yB[4]-yB[3])+yB[3]
text(x = 1.5,y = new_yB, "B", font = 2)
par("usr")
将 return 包含 4 个数字的向量:xLow、xHigh、yLow、yHigh