本文介绍了ggplot2中的一个方向错误栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在绘制一些干预后重复测试的数据。因此,我想用误差线将结果绘制在同一图上。由于两次试验的误差线相交,难以破译,因此我只想为后期测试向上绘制误差线,而仅在预测试时向下绘制误差线。 这可能在ggplot2中吗? 示例代码: dat.-data.frame(Trial = c(rep(Pre,9),rep(Post,9) )),时间= rep.int(seq(0,120,15),2),胰岛素= c(rnorm(9,15,2),rnorm(9,22,2)), Insulin_sd = c(rnorm(18,3,1))) p3 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(限制= c(0,35)) (抱歉 - 无法上传图片) 非常感谢。解决方案接近你想要的一种方法是使用基于布尔逻辑的手动设置错误栏最大值和最小值在你现有的数据上。 $ $ $ $ $ $ $ $ $ dat $胰岛素 - (dat $ Trial =='Pre')* dat $ Insulin_sd dat $ max p3 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(固体 ,虚线), guide = guide_legend(reverse = TRUE))+ scale_y_continuous(limits = c(0,35)) I am plotting some data from a test that was repeated pre and post an intervention. As such I would like to plot the results on the same figure with error bars. As the error bars of the two trials cross, making it difficult to decipher, I would like to plot the error bars upwards only for the post test and downwards only for the pre test.Is this possible in ggplot2?Example code: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))(sorry- not able to upload an image)Any help greatly appreciated. 解决方案 One way to get close to what you want is to manually set error bar maxima and minima using Boolean logic based on your existing data.dat$min <- dat$Insulin - (dat$Trial=='Pre')*dat$Insulin_sddat$max <- dat$Insulin + (dat$Trial=='Post')*dat$Insulin_sdp3 <- 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)) 这篇关于ggplot2中的一个方向错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 13:49