r - 使用基本 R 图并排绘制两个图,具有共享标题和共享 x 轴标签

标签 r plot yaxis x-axis

我正在尝试使用基本 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 轴标签放在两个图的中心。

现在看起来像这样:

enter image description here

最佳答案

这并不太复杂。我认为您正在寻找

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/

相关文章:

arrays - R中数组的简单子(monad)集选择

r - 如何从多个绘图输出中选择单个图

r - R:将校准轴添加到ggplot2中的PCA双图

R:如何停止在绘图图表上将百分比四舍五入到小数点后 0 位?

R | ggplot2 | (删除刻度线 + 删除面板边框)但保留轴线

ios - 图表 3.0 - 无法在条形图中显示小数 - iOS Swift

r - 在 Ubuntu 16.04 中安装 R 3.0.2

r - 组不互斥时类似于 group_by 的功能

r - 将单列传递给 j 和从 data.table 中选择同一列有什么区别?

r - 供傻瓜使用的黑白马赛克图/网格图