将来自不同模型的系数估计值与线连接起来

Connecting coefficient estimates from different models with line

我有来自三个不同模型的结果,我想按模型绘制和分组。在我的情节中,我想通过将点估计值与点连接起来来模拟折线图。我图中的 y 轴是系数估计值或 OR,x 轴表示(预测变量的)十分位数。我尝试了下面的代码,但没有得到想要的结果

PM2.5 %>% 
  ggplot(aes(x=Deciles, y=`Odds ratio`, colour=Model)) + 
  geom_hline(yintercept = 1) + 
  geom_point(position = position_dodge(width=.75)) + 
  geom_errorbar(aes(ymin = lci, ymax=uci), position=position_dodge(width=.75), height=0) + 
  labs(x="Deciles", y="Odds Ratio", colour="Model") +  geom_line(aes(colour = Model), linetype = 1) + 
  theme_classic()

这是我从上面的代码中得到的

然而,我的愿望是得到类似的东西

我的数据的可重现示例是

structure(list(Exposure = c("PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5"), Model = c("Model 1", 
"Model 1", "Model 1", "Model 1", "Model 1", "Model 1", "Model 1", 
"Model 1", "Model 1", "Model 1", "Model 2", "Model 2", "Model 2", 
"Model 2", "Model 2", "Model 2", "Model 2", "Model 2", "Model 2", 
"Model 2", "Model 3", "Model 3", "Model 3", "Model 3", "Model 3", 
"Model 3", "Model 3", "Model 3", "Model 3", "Model 3"), Deciles = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"), 
    `Odds ratio` = c(1, 1.13671841372967, 1.1664164497357, 1.184538501308, 
    1.01426983107309, 1.02031038009218, 1.01477731473527, 0.875918108929801, 
    0.93873719845089, 0.910192498666819, 1, 1.10517608899538, 
    1.05698136307814, 1.06082222001504, 0.973729945909015, 0.902027577793767, 
    0.859298771644556, 0.803309090738125, 0.824361076317661, 
    0.72883335118702, 1, 1.11378854166892, 1.06668615269094, 
    1.07899041127445, 0.995885073453648, 0.927986616376051, 0.888968869998538, 
    0.837609596281666, 0.864957178568897, 0.769471822581565), 
    sd = c(0, 0.0625482346458802, 0.0674859594948194, 0.0727757812357865, 
    0.0794093586577233, 0.082512540205002, 0.0883729567009082, 
    0.0940732398304658, 0.0983500597017137, 0.105403337124216, 
    0, 0.0637547687852429, 0.0687923642451317, 0.0741061092046816, 
    0.0804186337085149, 0.0839381076214092, 0.0897820413121509, 
    0.095465473697951, 0.100061134069393, 0.107576493145057, 
    0, 0.0638630241818262, 0.0688985051442568, 0.0742037952183392, 
    0.0804084609538821, 0.0839052298120131, 0.0896485066371262, 
    0.0952499887303732, 0.0998030071577366, 0.107276812857393
    ), lci = c(1, 1.00556868748125, 1.0219025501156, 1.02707539200597, 
    0.868080776957615, 0.867955587714278, 0.853390006711178, 
    0.728430661754372, 0.774155680754043, 0.740310260683707, 
    1, 0.975356365359843, 0.923657930007991, 0.917409796456157, 
    0.831737076050997, 0.765194022442772, 0.720645333716221, 
    0.666227144127305, 0.677556141033298, 0.590281212400027, 
    1, 0.98274861614993, 0.931944701181661, 0.93294319280486, 
    0.850678422544112, 0.787265920045722, 0.745723116461695, 
    0.694967838483058, 0.711282516350223, 0.623560407658435), 
    uci = c(1, 1.2849731382832, 1.33136700173624, 1.36614261426375, 
    1.18507783783729, 1.19940845644575, 1.2066850916967, 1.05326776292384, 
    1.13830790067586, 1.11905835786234, 1, 1.252274790083, 1.20954908261857, 
    1.22665333074129, 1.13996361934674, 1.0633299885211, 1.02462937648135, 
    0.968596823096727, 1.00297398693965, 0.8999067607839, 1, 
    1.2623013608637, 1.22090865144671, 1.24790053306678, 1.16587779029548, 
    1.09386058540812, 1.05973066193271, 1.00952849460564, 1.05183369977492, 
    0.949526106011721)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))
> dput(PM2.5)
structure(list(Exposure = c("PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5", 
"PM2.5", "PM2.5", "PM2.5", "PM2.5", "PM2.5"), Model = c("Model 1", 
"Model 1", "Model 1", "Model 1", "Model 1", "Model 1", "Model 1", 
"Model 1", "Model 1", "Model 1", "Model 2", "Model 2", "Model 2", 
"Model 2", "Model 2", "Model 2", "Model 2", "Model 2", "Model 2", 
"Model 2", "Model 3", "Model 3", "Model 3", "Model 3", "Model 3", 
"Model 3", "Model 3", "Model 3", "Model 3", "Model 3"), Deciles = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"), 
    `Odds ratio` = c(1, 1.13671841372967, 1.1664164497357, 1.184538501308, 
    1.01426983107309, 1.02031038009218, 1.01477731473527, 0.875918108929801, 
    0.93873719845089, 0.910192498666819, 1, 1.10517608899538, 
    1.05698136307814, 1.06082222001504, 0.973729945909015, 0.902027577793767, 
    0.859298771644556, 0.803309090738125, 0.824361076317661, 
    0.72883335118702, 1, 1.11378854166892, 1.06668615269094, 
    1.07899041127445, 0.995885073453648, 0.927986616376051, 0.888968869998538, 
    0.837609596281666, 0.864957178568897, 0.769471822581565), 
    sd = c(0, 0.0625482346458802, 0.0674859594948194, 0.0727757812357865, 
    0.0794093586577233, 0.082512540205002, 0.0883729567009082, 
    0.0940732398304658, 0.0983500597017137, 0.105403337124216, 
    0, 0.0637547687852429, 0.0687923642451317, 0.0741061092046816, 
    0.0804186337085149, 0.0839381076214092, 0.0897820413121509, 
    0.095465473697951, 0.100061134069393, 0.107576493145057, 
    0, 0.0638630241818262, 0.0688985051442568, 0.0742037952183392, 
    0.0804084609538821, 0.0839052298120131, 0.0896485066371262, 
    0.0952499887303732, 0.0998030071577366, 0.107276812857393
    ), lci = c(1, 1.00556868748125, 1.0219025501156, 1.02707539200597, 
    0.868080776957615, 0.867955587714278, 0.853390006711178, 
    0.728430661754372, 0.774155680754043, 0.740310260683707, 
    1, 0.975356365359843, 0.923657930007991, 0.917409796456157, 
    0.831737076050997, 0.765194022442772, 0.720645333716221, 
    0.666227144127305, 0.677556141033298, 0.590281212400027, 
    1, 0.98274861614993, 0.931944701181661, 0.93294319280486, 
    0.850678422544112, 0.787265920045722, 0.745723116461695, 
    0.694967838483058, 0.711282516350223, 0.623560407658435), 
    uci = c(1, 1.2849731382832, 1.33136700173624, 1.36614261426375, 
    1.18507783783729, 1.19940845644575, 1.2066850916967, 1.05326776292384, 
    1.13830790067586, 1.11905835786234, 1, 1.252274790083, 1.20954908261857, 
    1.22665333074129, 1.13996361934674, 1.0633299885211, 1.02462937648135, 
    0.968596823096727, 1.00297398693965, 0.8999067607839, 1, 
    1.2623013608637, 1.22090865144671, 1.24790053306678, 1.16587779029548, 
    1.09386058540812, 1.05973066193271, 1.00952849460564, 1.05183369977492, 
    0.949526106011721)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

为了能够在 Deciles 上使用 geom_line(),同时将其作为因子变量,您可以使用 as.numeric(as.character(Deciles)).

此外,因为你在geom_point()geom_errorbar()中都使用了position_dodge,所以你需要在geom_line()中也使用它,同样的width ],否则生成的线将不会与误差线中的点对齐。

实现如下:

PM2.5 %>% 
    ggplot(aes(x=Deciles, y=`Odds ratio`, colour=Model)) + 
    geom_hline(yintercept = 1) + 
    geom_point(position = position_dodge(width=.75)) + 
    geom_errorbar(aes(ymin = lci, ymax=uci),    
                  position = position_dodge(width=.75)) + 
    labs(x="Deciles", y="Odds Ratio", colour = "Model") + 
    geom_line(aes(colour = Model), linetype = 1) + 
    theme_classic() + 
    geom_line(aes(x = as.numeric(as.character(Deciles))), 
              position = position_dodge(width=.75))

结果: