【问题标题】:Combine 2 Predicted Probabilities graphs in R with SjPlot Package将 R 中的 2 个预测概率图与 SjPlot 包相结合
【发布时间】:2018-07-19 13:37:38
【问题描述】:

假设我有这个代码:

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)),但它根本不起作用。

任何帮助表示赞赏!

【问题讨论】:

  • 您是想将它们叠加起来,还是将它们并排放在一张图片中?
  • sjPlot 中有一个plot_grid() 函数。这对你有帮助吗?

标签: r plot logistic-regression sjplot


【解决方案1】:

由于您尝试了par() 函数,我假设您想要并排创建两个图表。您可以使用库gridExtragrid.arrange() 函数实现此目的,如下所示:

使用的代码:

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

【讨论】:

  • 感谢您的快速回复。还有一个问题:如何添加“物流”之类的标题。它不应出现在两个图上,而应显示在组合图的开头。
  • @AmelioTornincasa 使用 top="string" 参数。 grid.arrange(m, n, ncol = 1, heights = c(1, 1), top="Logistic")
  • @AmelioTornincasa 如果您还添加了库(网格),您可以使用 top=textGrob("Title", gp=gpar(fontsize=19,font=8)) 更改标题的大小
猜你喜欢
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多