使用R base创建子图

时间:2019-10-11 13:42:59

标签: r plot r-base

link展示了使用多个程序包创建并排图的良好示例。但是,我只想使用基本函数在R中创建(4 x 1)图。

这是我尝试过的:

par(mar=c(0.2, 0.2, 0.7, 0.7), mfrow=c(4,1), oma = c(4, 4, 0.2, 0.2))
# Plot 1
plot(my_data$date, my_data$col1, type="l", col = "red", ylab = expression("my_legend"^{-5}), xlab = "", xaxt="nan", lwd = 1)
lines(my_data$date, my_data$col2, type="l", col = "blue", lwd = 1)
legend(14, 20, legend=c("Line 1", "Line 2"), col=c("red", "blue"), lty=1:2, cex=0.8)
# Plot 2
plot(my_data$date, my_data$col3, type="l", col = "magenta",xlab = "", xaxt = "nan", ylab = expression("my_legend"^{2}))
lines(my_data$date, my_data$col4, type="l", col = "green")
legend(14, 20, legend=c("Line 3", "Line 5"), col=c("orange", "yellow"), lty=1:2, cex=0.8)
# Plot 3
plot(my_data$date, my_data$col5, type="l", col = "olivedrab2",xlab = "", xaxt = "nan", ylab = expression("my_legend"^{8}))
# Plot 4
plot(my_data$date, my_data$col6, type="l", col = "sandybrown", xlab = "Time (3 May 1994)", ylab = expression("my_legend"^{7}))

我想要y轴标题(所有四个图)和一个通用的x轴标题(图4)。 即,不显示y轴标题(此处为图1),也未显示x轴标题(图4)和图例(图1内)。有人可以帮我理解为什么吗?

1 个答案:

答案 0 :(得分:1)

您可能会发现此链接很有用。

https://stevencarlislewalker.wordpress.com/2012/06/28/overall-axis-labels-with-mfrow/

如果链接消失,我将在此处复制代码,但是您应该阅读他的文章。截至2019年,它仍然可用。想法是使用xaxt='n'yaxt='n'在没有轴的情况下进行绘图,然后使用mtext通过注释图的边距来标记“总体”轴。

我仍然认为ggplot可以为您提供更多功能,也许您的公司不允许您安装它?

# thanks to 
# https://stevencarlislewalker.wordpress.com/2012/06/28/overall-axis-labels-with-mfrow/
par(mfrow = c(2, 2)) # 2-by-2 grid of plots
par(oma = c(4, 4, 0, 0)) # make room (i.e. the 4's) for the overall x and y axis titles
par(mar = c(2, 2, 1, 1)) # make the plots be closer together

# now plot the graphs with the appropriate axes removed (via xaxt and yaxt),
# remove axis labels (so that they are not redundant with overall labels,
# and set some other nice choices for graphics parameters
plot(runif(10), xlab = '', ylab = '', xaxt = 'n', las = 1, ylim = c(0, 1))
plot(runif(10), xlab = '', ylab = '', xaxt = 'n', yaxt = 'n', ylim = c(0, 1))
plot(runif(10), xlab = '', ylab = '', las = 1, ylim = c(0, 1))
plot(runif(10), xlab = '', ylab = '', yaxt = 'n', ylim = c(0, 1))

# print the overall labels
mtext('x-axis title', side = 1, outer = TRUE, line = 2)
mtext('y-axis title', side = 2, outer = TRUE, line = 2)