假设我有以下数据集
term estimate std.error statistic p.value conf.low conf.high df outcome model
outgroup_pairing1 9.173850 2.336151 3.926908 0.0001574491 4.539555 13.808145 101 outgroup_feelings_diff 1
outgroup_pairing1 11.663866 3.330378 3.502265 0.0006886152 5.057292 18.270440 101 outgroup_feelings_diff 2
politicsRepublican:outgroup_pairing1 -5.002525 4.645316 -1.076897 0.2840919026 -14.217582 4.212531 101 outgroup_feelings_diff 2
outgroup_pairing1 10.657113 3.575874 2.980282 0.0036079930 3.563540 17.750686 101 outgroup_feelings_diff 3
politicsRepublican:outgroup_pairing1 -4.928449 4.647266 -1.060505 0.2914443020 -14.147374 4.290476 101 outgroup_feelings_diff 3
outgroup_pairing1 10.512772 4.162351 2.525681 0.0131016794 2.255788 18.769757 101 outgroup_feelings_diff 4
politicsRepublican:outgroup_pairing1 -5.359123 4.978743 -1.076401 0.2843123953 -15.235609 4.517363 101 outgroup_feelings_diff 4
我希望能够输出主要效果,相互作用系数及其伴随的误差线,并按型号进行分组。这是我的最佳尝试:
ggplot(aes(x = estimate, y = model, color = term, group= term)) +
geom_vline(xintercept = 0, linetype = 2) +
geom_point(position="dodge") +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high, height = 0.1),position="dodge") +
ggtitle("Change in Outgroup Warmth") +
geom_text(aes(label = paste("β = ", round(estimate, 2), "; p = ", round(p.value, 5)), vjust = -.5)) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5)) +
ylab("Model") +
xlab("Estimate")
但这产生了以下结果。
我希望系数水平“躲避”,这样它们就不会像那样重叠。我也希望删除图例中的“a”。
最佳答案
您无法在当前方向上进行躲避,仅可在x方向上进行躲避。但是您可以使用coord_flip
并以其他方式定义所有内容:
pos <- position_dodge(width = 0.5)
ggplot(df, aes(y = estimate, x = model, color = term, group= term)) +
geom_hline(yintercept = 0, linetype = 2) +
geom_point(position = pos) +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width = 0.1), position = pos) +
ggtitle("Change in Outgroup Warmth") +
geom_text(aes(label = paste("β = ", round(estimate, 2), "; p = ", round(p.value, 5)), vjust = -.5),
position = pos, show.legend = FALSE) +
coord_flip() +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5)) +
ylab("Model") +
xlab("Estimate")
另请注意,您的
show.legend = FALSE
中的geom_text
不会在图例中显示文本(“a”)。关于r - 结合使用带有geom_point和误差线的躲避功能,水平显示模型中的系数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59888853/