我有一个条形图,我需要添加一个错误条。它应该是一个错误范围,每个列都是相同的,因此与标准错误条略有不同。在前面的问题中,只讨论了依赖于均值或标准差的误差条。
我试过箭头函数
arrows(dat$usage_time, dat$usage_time-1, dat$usage_time, dat$usage_time+1, length=0.05, angle=90, code=3)
但这没什么用。dat$usage_time
是一个整数,它应该作为一个坐标。有什么问题吗?
发布于 2018-09-30 20:13:42
是的,你需要提供数据和代码。然而,我们将用我们所拥有的进行工作。
第一个选项是从这里修改的:假设您的错误条为+/-1,并且使用虚拟数据集:https://datascienceplus.com/building-barplots-with-error-bars/:
x<-c(1,1,1, 1, 2,2,2)
y<-c(4,8,12,12,5,3,3)
d<-as.data.frame(cbind(x,y))
library(dplyr)
d2<- d %>% group_by(x) %>% summarise_at(mean, .vars = vars(y))
barplot<-barplot(height=d2$y, ylim=c(0, max(d2$y)+3))
text(x = barplot, y = par("usr")[3] - 1, labels = d2$x)
arrows(barplot, d2$y-1, barplot, d2$y+1, length=0.05, angle=90, code=3)
要在ggplot2
中绘制这幅图,不如:
ggplot(data=d2, aes(x=x, y=y)) +
geom_bar(fill="grey", width=.8, stat="identity") +
xlab("date") + ylab("usage time") +
geom_errorbar(aes(ymin=y-1, ymax=y+1), width=.2)
https://stackoverflow.com/questions/52582326
复制