2

可以说我有这个代码:

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))已经尝试过了,但它根本不起作用。

任何帮助表示赞赏!

4

1 回答 1

2

由于您尝试了该par()功能,我假设您想要并排创建两个图表。您可以使用如下所示 grid.arrange()的库功能来实现这一点: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))

如果您希望它们并排而不是一个在另一个之上,只需将ncol值更改为 2

于 2018-07-19T14:07:49.383 回答