ggplot2:用回归系数绘制两个模型

cib*_*ibr -1 r ggplot2

我想复制下面的图ggplot2(虽然更大,但我不知道为什么这里的图这么小)。

在 Stata 中开发的原始图

(本研究的主题是穆斯林少数民族青年的融入。)

我在 Stata 中使用 -coefplot- 包开发了上面的原始图,现在尝试在 R 中进行相同的操作。

在 R 和 中,我从使用(来自 Mplus 应用程序中的估计)ggplot2获得的两个简单数据帧开始。MplusAutomation可以使用以下代码重现数据帧:

resModel1 <- 
  structure(list(param = c("FEMALE", "CHRISTIAN", "MUSLIM"), 
  low2.5 = c(-0.436, 0.038, 0.19), 
  est = c(-0.271, 0.18, 0.354), up2.5 = c(-0.106, 0.323, 0.519)), 
  .Names = c("param", "low2.5", "est", "up2.5"), row.names = 7:9, 
  class = c("data.frame", "mplus.params"))

resModel2 <- 
  structure(list(param = c("FEMALE", "CHRISTIAN", "MUSLIM", "CHRISTGI", "MUSLIMGI"), 
  low2.5 = c(-0.672, -0.256, -0.018, 0.131, -0.143), 
  est = c(-0.437, -0.038, 0.237, 0.403, 0.237), up2.5 = c(-0.203,                                                                                 0.18, 0.493, 0.675, 0.617)), .Names = c("param", "low2.5", "est", "up2.5"), 
  row.names = 7:11, class = c("data.frame", "mplus.params"))
Run Code Online (Sandbox Code Playgroud)

然后我尝试用ggplot2和 来开发两个图并将它们组合起来gridExtra

library(ggplot2)
limits <- aes(ymax  = up2.5, ymin = low2.5) 

plot1 <- ggplot(resModel1, aes(x=param, y=est)) + 
  geom_pointrange(limits, color = "darkblue") + 
  scale_x_discrete("") + 
  geom_hline(yintercept=0, color="red") + 
  theme_bw() + 
  theme(text = element_text(size=10)) +
  ylab(NULL) + 
  coord_flip() 

plot2 <- ggplot(resModel2, aes(x=param, y=est)) + 
  geom_pointrange(limits, color="darkblue") + 
  scale_x_discrete("") + 
  geom_hline(yintercept=0, color="red") + 
  theme_bw() + 
  theme(text = element_text(size=10)) +
  ylab(NULL) + 
  coord_flip()

library(gridExtra)
grid.arrange(plot1, plot2, ncol=2)
Run Code Online (Sandbox Code Playgroud)

...并最终得到以下情节:

在此输入图像描述

我希望做到以下几点:

  1. 手动对系数进行排序(按此顺序:基督教、穆斯林、女性)
  2. 跨模型对齐相同预测变量的系数(如在 Stata 中开发的绘图中所做的那样,其中绘制了“穆斯林女孩”和“基督教女孩”,但在 Plot1 中没有结果,使得所有预测变量/参数在模型 1 和模型 2 中对齐)
  3. 在图中包含回归系数(点上方的数字代表回归分析中的点估计)。
  4. 更改预测变量的名称(例如,从 MUSLIMGI 更改为“穆斯林女孩”),而不编辑数据框。
  5. 将两个图的 x 比例调整为相同。(另外,说出这两个模型的名称,我想我应该能够弄清楚自己如何做到这一点。)

在寻找解决方案时,我发现了一个名为coefplot(的一种附加组件ggplot2)的 R 包,但我不确定是否coefplot可以开发我想要的图,并且我想自己编写代码以ggplot2提高灵活性。

我还发现sjp.glmhttps://strengejacke.wordpress.com/2013/03/22/plotting-lm-and-glm-models-with-ggplot-rstats/),但当我已经估计了 R 之外的统计模型,并且需要绘制数据框中可用的参数。

Axe*_*man 5

至少为了让你开始,修复第 1、2、3 和 5 点。第 4 点是不可取的,编辑 data.frame 会容易得多,不要人为地限制自己。

df <- rbind(resModel1, resModel2)
df$model <- rep(c('Model 1', 'Model 2'), c(nrow(resModel1), nrow(resModel2)))
df$param_f <- factor(df$param, 
                     rev(c('CHRISTIAN', 'MUSLIM', 'FEMALE', 'CHRISTGI', 'MUSLIMGI')),
                     rev(c('Christian', 'Muslum', 'Female', 'Christian girls', 'Muslim girls')))

ggplot(df, aes(x = param_f, y = est)) + 
  geom_pointrange(aes(ymax = up2.5, ymin = low2.5), color = "darkblue") +
  geom_text(aes(label = est), nudge_x = 0.15) + 
  scale_x_discrete("") + 
  geom_hline(yintercept = 0, color = "red") + 
  theme_bw() + 
  theme(text = element_text(size=10)) +
  ylab(NULL) + 
  coord_flip() +
  facet_grid(~model)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述