2017-03-02 127 views
-1

我想複製下面的圖ggplot2(儘管更大,我不知道爲什麼這個數字這麼小)。ggplot2:用迴歸係數繪製兩個模型

Original graph developed in Stata

(這項研究的對象是穆斯林少數民族青年的融合。)

我上面開發與包-coefplot-在Stata現在原圖嘗試在R上的相同

在R和ggplot2中,我從用MplusAutomation(來自Mplus應用程序中的估計)獲得的兩個簡單數據幀開始。數據幀可與下面的代碼重新:

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")) 

然後我嘗試開發兩個地塊與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) 

...並以下列結束劇情:

enter image description here

我希望能做到以下幾點:

用手個
  1. 排序係數(順序:基督徒,穆斯林,女)
  2. 對準係數在模型相同的預測(如在Stata的,開發的地塊進行,其中「穆斯林女孩」和「基督教女孩「在繪圖1中沒有結果,使得所有的預測因子/參數在模型1和模型2上對齊)
  3. 在圖中包括迴歸係數(迴歸分析中代表點估計值的點上方的數字)。
  4. 更改預測變量的名稱(例如,從MUSLIMGI改爲「穆斯林女孩」),無需編輯數據框。
  5. 調整兩個地塊的x尺度相同。 (另外,命名兩款車型,我想我應該能夠弄清楚自己該怎麼做。)

當解決方案搜索,我發現的R包叫做coefplot(一種附加的到ggplot2),但我不確定coefplot是否可以開發我想要的情節,並且我想自己編寫代碼以獲得靈活性ggplot2

我還發現sjp.glmhttps://strengejacke.wordpress.com/2013/03/22/plotting-lm-and-glm-models-with-ggplot-rstats/),但是當我估計出R之外的統計模型並且需要繪製數據框中可用的參數時,我無法看到該功能如何幫助我。

+2

你需要在一個時間問問題只有一個。做一個最基本的例子,重點關注你需要做的一件事。這5個請求中的每一個都可能存在問題和答案。當你把它們全部放在一起時,很難指出你在正確的位置。這些問題將在未來幫助其他求助者。 – MrFlick

+0

感謝您的評論。我認爲這些問題可以幫助其他人制定類似的情節,部分原因是他們會在一個地方找到他們大部分/全部的答案。 – cibr

回答

3

爲了讓你至少開始,修復點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) 

enter image description here

+0

非常好!非常感謝你! – cibr