我在ggplot2作業。
我創建了一個點范圍圖表,但標簽沒有直接以點為中心(參見下圖和代碼)。
如何在此圖中將每個標簽集中在其各自的點上?

這是資料:
DF <- structure(list(PID_3 = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Democrat",
"Republican"), class = "factor"), AME = c(0.0943872438937147,
-0.145952449974528, 0.109719848369749, -0.154140815079052, 0.114979740556292,
-0.161639689331601, 0.106980748082516, -0.0836619230140896, 0.186783558306622,
-0.0980531799510613, 0.16778336838723, -0.126273012255784, 0.0906715314499834,
-0.170941497211481, 0.106281566095293, -0.182241300474391, 0.110681329466068,
-0.192725676443545), p = c(1.2187220809442e-10, 4.24409004476721e-14,
3.96045837743953e-10, 2.54601555265504e-14, 0.00000411016648101134,
0.00109656724866882, 1.17877696845627e-09, 0.0000224748623520196,
4.87470706090971e-09, 0.829271026386558, 0.315839668276529, 0.935491536818306,
2.63789672610624e-11, 6.36448293002342e-15, 1.28932806704258e-10,
1.52325910151201e-15, 0.000410617778549237, 0.00228729546644436
), lower = c(0.0656477519264177, -0.183824909086534, 0.0753430178919607,
-0.193790302191668, 0.0660502478297795, -0.258686031649377, 0.0725117653015181,
-0.122346350111422, 0.124219139634654, -0.989278063580894, -0.160068035715313,
-3.18406567681799, 0.064009872119084, -0.213914488023734, 0.0738773550312702,
-0.227029311977117, 0.049282700952277, -0.316567383457244), upper = c(0.123126735861012,
-0.108079990862523, 0.144096678847537, -0.114491327966435, 0.163909233282805,
-0.064593347013824, 0.141449730863515, -0.0449774959167572, 0.249347976978591,
0.793171703678772, 0.495634772489772, 2.93151965230642, 0.117333190780883,
-0.127968506399227, 0.138685777159316, -0.137453288971664, 0.172079957979858,
-0.068883969429847), Model = c("Base", "Base", "Control", "Control",
"Fixed Effects", "Fixed Effects", "Base", "Base", "Control",
"Control", "Fixed Effects", "Fixed Effects", "Base", "Base",
"Control", "Control", "Fixed Effects", "Fixed Effects"), Group = c("Thermometer",
"Thermometer", "Thermometer", "Thermometer", "Thermometer", "Thermometer",
"Candidate", "Candidate", "Candidate", "Candidate", "Candidate",
"Candidate", "Word", "Word", "Word", "Word", "Word", "Word")), class = "data.frame", row.names = c(NA,
-18L))
這是情節代碼
ggplot(DF, aes(x = Group , y = AME, color = PID_3 , ymin = lower, ymax = upper, label = round(AME,2)))
facet_wrap(Model ~ ., scale = "free_x" )
geom_pointrange(position=position_dodge(width=.5))
coord_flip()
geom_label()
geom_hline(yintercept = 0, linetype = "dashed", color = "grey")
guides(color="none")
theme_bw()
labs(title = "Average Marginal Effects of Affective Polarization on the Likelihood of Biden Vote",
subtitle = "Across Party Identification and Alternative Measures of Affective Polarization",
y = "Average Marginal Effects",
caption= "Note: Point estimates are dots with lines indicating 95 percent confidence intervals.\nModel: Logistic Regression.\nAverage marginal effects calculated using sd")
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption= element_text(hjust = 0),
axis.title.y=element_blank())
謝謝你的幫助,我真的很感激!
uj5u.com熱心網友回復:
您可以在呼叫中傳遞position引數geom_label。只需將其設定為position_dodge(width= 0.4).
代碼如下所示:
ggplot(DF, aes(x = Group , y = AME, color = PID_3 , ymin = lower, ymax = upper, label = round(AME,2)))
facet_wrap(Model ~ ., scale = "free_x" )
geom_pointrange(position = position_dodge(width= 0.4))
coord_flip()
geom_label(position = position_dodge(width = 0.9))
geom_hline(yintercept = 0, linetype = "dashed", color = "grey")
guides(color="none")
theme_bw()
labs(title = "Average Marginal Effects of Affective Polarization on the Likelihood of Biden Vote",
subtitle = "Across Party Identification and Alternative Measures of Affective Polarization",
y = "Average Marginal Effects",
caption= "Note: Point estimates are dots with lines indicating 95 percent confidence intervals.\nModel: Logistic Regression.\nAverage marginal effects calculated using sd")
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption= element_text(hjust = 0),
axis.title.y=element_blank())
和輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416792.html
標籤:
