我正在绘制干预前后重复测试的一些数据。因此,我想用错误条在同一图上绘制结果。由于两个试验的误差条交叉,使得难以破译,我想将误差条仅向上绘制为后测试,向下仅用于预测试。 这可能在ggplot2中吗?
示例代码:
library("ggplot2")
set.seed(1)
dat <- data.frame(Trial = c(rep("Pre",9),rep("Post",9)),
Time = rep.int(seq(0,120,15),2),
Insulin = c(rnorm(9,15,2),rnorm(9,22,2)),
Insulin_sd = c(rnorm(18,3,1)))
p3 <- ggplot(data = dat, aes(x = Time, y = Insulin, group = Trial))+
geom_errorbar(aes(ymin = Insulin - Insulin_sd, ymax = Insulin + Insulin_sd,linetype = Trial), width = 4) +
geom_line(aes(linetype = Trial)) +
geom_point(aes(shape= Trial, fill = Trial), size = 2) +
scale_shape_manual(values=c(21,24),guide = guide_legend(reverse = TRUE)) +
scale_fill_manual(values=c("black","white"),guide = guide_legend(reverse = TRUE)) +
scale_linetype_manual(values = c("solid","dashed"),guide = guide_legend(reverse = TRUE)) +
scale_y_continuous(limits= c(0,35))
(抱歉 - 无法上传图片)
非常感谢任何帮助。
答案 0 :(得分:3)
接近您想要的一种方法是使用基于现有数据的布尔逻辑手动设置误差线最大值和最小值。
dat$min <- dat$Insulin - (dat$Trial=='Pre')*dat$Insulin_sd
dat$max <- dat$Insulin + (dat$Trial=='Post')*dat$Insulin_sd
p3 <- ggplot(data = dat, aes(x = Time, y = Insulin, group = Trial)) +
geom_errorbar(aes(ymin=min, ymax=max, linetype = Trial), width = 4) +
geom_line(aes(linetype = Trial)) +
geom_point(aes(shape= Trial, fill = Trial), size=2) +
scale_shape_manual(values=c(21,24), guide=guide_legend(reverse = TRUE)) +
scale_fill_manual(values=c("black","white"), guide=
guide_legend(reverse = TRUE)) +
scale_linetype_manual(values = c("solid","dashed"),
guide=guide_legend(reverse = TRUE)) +
scale_y_continuous(limits= c(0,35))