将R中的2个预测概率图与SjPlot程序包组合

时间:2018-07-19 13:37:39

标签: r plot logistic-regression sjplot

让我说我有这段代码:

dt <- data.frame(x = (1:9), z = c(6,5,3,7,8,3,1,6,7), y = c(1,0,1,1,0,0,0,1,0))
model1 <- glm(y ~ x + z, family = binomial(link = "logit"),
              data = dt)
model2 <- glm(y ~ x + z, family = binomial(link = "logit"),
              data = dt)
summary(model1)
summary(model2)
library(sjPlot)
m <- plot_model(model1, title = "Predicted probabilities", type = "pred", terms = "x")
n <- plot_model(model2, title = "Predicted probabilities", type = "pred", terms = "x")

我想将两个图形组合起来,所以我可以将它们保存在一个文件中。在两个图中,您都可以从sjPlot包中找到预测的概率。

我该如何实现?

我已经尝试过par(mfrow=c(2,2)),但是它根本不起作用。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

自从您尝试使用par()函数以来,我假设您想并排创建两个图形。您可以使用库grid.arrange()的{​​{1}}函数来实现此目的,该函数如下所示: gridExtra

使用的代码:

gridExtra

如果您希望它们并排,而不是一个在另一个之上,只需将library(sjPlot) library(gridExtra) dt <- data.frame(x = (1:9), z = c(6,5,3,7,8,3,1,6,7), y = c(1,0,1,1,0,0,0,1,0)) model1 <- 0 model2 <- 0 model1 <- glm(y ~ x + z, family = binomial(link = "logit"), data = dt) model2 <- glm(y ~ x^4 + z, family = binomial(link = "logit"), data = dt) summary(model1) summary(model2) m <- plot_model(model1, title = "Predicted probabilities", type = "pred", terms = "x") n <- plot_model(model2, title = "Predicted probabilities", type = "pred", terms = "x") grid.arrange(m, n, ncol = 1, heights = c(1, 1)) 的值更改为2