![](/img/trans.png)
[英]Overlay points (and error bars) over bar plot with position_dodge
[英]mapping error bars to geom_points with position_dodge
鉴于 dataframe:
df1 <- data.frame(F = c(0.200, 0.200, 0.130, 0.130, 0.394, 0.394, 0.067,
0.067, 0.591, 0.591, 0.200, 0.200, 0.130, 0.130, 0.394),
lci = c(156854, 222629, 112382, 180782, 241555, 284076, 63481,
120508, 291506, 315274, 116201, 165269, 83254, 134403,
178948),
median = c(182955, 259094, 131078, 209884, 281751, 331179, 74039,
139238, 339989, 367623, 135226, 191810, 96878, 155431,
208302),
uci = c(213542, 302074, 152997, 244224, 328853, 386617, 86423,
161363, 396857, 429202, 158196, 223485, 113343, 180521,
243621),
mr = c("H", "M", "H", "M", "H", "M", "H", "M", "H", "M", "H",
"M", "H", "M", "H"),
start = c("S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "M",
"M", "M", "M", "M"))
我想覆盖误差线。 我可以使用下面的代码来做到这一点,但误差线不会与点水平对齐。 如何确保误差线通过相应点的中心? 代码:
ggplot(df1) +
geom_point(aes(x = as.factor(F),
y = median/1000,
shape = mr,
size = start,
color = mr),
position = position_dodge(width = 0.2) )+
geom_errorbar(aes(x = as.factor(F),
ymin = lci/1000,
ymax = uci/1000),
linetype = "solid",
width = .1,
position = position_dodge(width = 0.2)) +
theme_classic()
您应该将美学带入ggplot
部分,并将它们排除在geom
的aes
之外。 然后将在绘制点的位置绘制误差线。 使用太多的美学将再次匹配不同的几何形状。
也许您可以改用构面。
library(ggplot2)
ggplot(df1, aes(x=factor(F), y = median/1000,shape=mr, color =mr)) +
geom_point(position=position_dodge(width=.5)) +
geom_errorbar(
aes(ymin = lci/1000, ymax = uci/1000),
width = 0.5,
position=position_dodge(width=.5)) +
theme_classic()
ggplot(df1, aes(x=factor(F), y = median/1000,shape=mr, color =mr)) +
geom_point(position=position_dodge(width=.5)) +
geom_errorbar(
aes(ymin = lci/1000, ymax = uci/1000),
width = 0.5,
position=position_dodge(width=.5)) +
facet_wrap(~start)+
theme_classic()
谢谢,这很有帮助。 由于各种原因,我不想使用构面。 是否可以仅将大小用作 ggplot 部分中的点但不能修改错误栏的大小? 像这样的东西(但它不起作用)
ggplot(df1, aes(x=factor(F), y = median/1000, shape = mr, color = mr,
size = start)) +
geom_point(position=position_dodge(width=.5)) +
geom_errorbar( aes(ymin = lci/1000, ymax = uci/1000, size = NULL),
width = 0.5, position=position_dodge(width=.5)) +
theme_classic()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.