在一页上使用par绘制多个情节图

时间:2018-03-05 18:06:02

标签: r par

我正在尝试在基础R中使用par函数在一个页面上创建多个图。我正在使用库ggpubr中的ggscatter函数。我做了类似的事情,但它不起作用

library(ggpubr)
par(mfrow=c(2,2))
plot(ggscatter(df, x = "a1", y = "a2", add = "reg.line", conf.int = TRUE, cor.coef = TRUE, cor.method = "pearson",
               add.params = list(color = "blue")))
plot(ggscatter(df, x = "a1", y = "b1", add = "reg.line", conf.int = TRUE, cor.coef = TRUE, cor.method = "pearson",
               add.params = list(color = "blue")))

这应该在一页上绘制,第一行中有2个图形。但相反,它被绘制在不同的页面上。知道这里可能出现的问题

1 个答案:

答案 0 :(得分:2)

par()无法使用ggplot。使用ggarange()。尝试:

p1 <- ggscatter(df, x = "a1", y = "a2", add = "reg.line",
         conf.int = TRUE, cor.coef = TRUE, cor.method = "pearson",
         add.params = list(color = "blue"))
p2 <- ggscatter(df, x = "a1", y = "b1", add = "reg.line",
         conf.int = TRUE, cor.coef = TRUE, cor.method = "pearson",
         add.params = list(color = "blue"))

ggarrange(p1, p2, ncol = 2, nrow = 2)

点击此链接了解更多信息: https://www.r-bloggers.com/ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/