我正在尝试使用基本 R 并排绘制两个图。
我希望他们分享标题以及 x 轴和 y 轴标签。 但是,绘图的范围不同,所以我想保留标签的数量。
我发现有一些使用 par()
的解决方案,但是,它们似乎过于复杂。
我使用layout()
尝试了以下解决方案:
dev.off()
# Create a layout with two plots
layout(matrix(c(1, 2), nrow = 1))
# Create the first plot
plot(1:10, 1:10, main = "Plot 1", type = "l", col = "blue", xlab = "")
# Create the second plot without the x-axis
plot(1:10, (1:10)^2, main = "", type = "l", col = "red", xlab = "")
# Draw the x-axis in the middle of the two plots
axis(side = 1, at = 5, labels = FALSE)
# Add labels to the x-axis if needed
mtext("Common X-axis Label", side = 1, line = 2)
# Reset the layout
layout(1)
但是我无法将 x 轴标签和主标题置于图的中心? 关于如何做到这一点有什么想法吗?
我尝试使用 line = 2
将 x 轴标签放在两个图的中心。
现在看起来像这样:
最佳答案
这并不太复杂。我认为您正在寻找
outer = TRUE
这可以解决问题。
layout(matrix(c(1, 2), nrow = 1))
plot(1:10, 1:10, main = "", type = "l", col = "blue", xlab = "") # Create the first plot
plot(1:10, (1:10)^2, main = "", type = "l", col = "red", xlab = "") # Create the second plot
mtext("My Multiplot Title", side = 3, line = -2, outer = TRUE)
mtext("Common X-axis Label", side = 1, line = -2, outer = TRUE)
创建于 2023 年 10 月 18 日 reprex v2.0.2
关于r - 使用基本 R 图并排绘制两个图,具有共享标题和共享 x 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77315709/