'Connecting coefficient estimates from different models with line
I have results from three different models that I want to plot and group by the models. In my plot, I want to mimic a line chart by connecting the point estimates with points. The y-axis in my plot is the coefficient estimate or OR and the x-axis depicts deciles (of the predictor). I tried the code below and I did not get the desired result
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()
this is what I got from the above code
However, my desire is to get something similar to this
A reproducible example of my data is
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"))
Solution 1:[1]
In order to be able to use geom_line() on Deciles while keeping it as a factor variable, you can use as.numeric(as.character(Deciles)).
In addition, because you use position_dodge in both of geom_point() and geom_errorbar(), you need to use it too in geom_line() with the same width, or otherwise the generated lines will not align well with the points in the error bars.
Here is the implementation:
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))
The result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abdur Rohman |



