#import data
data = diameters$V1
error = .005 #mm
#make histogram
h <- hist(data, breaks = "FD", density = 10,
col = "lightblue", xlab = "Diameter", main = "Overall")
# Make normal curve
xfit <- seq(min(data), max(data), length = 40)
yfit <- dnorm(xfit, mean = mean(data), sd = sd(data))
yfit <- yfit * diff(h$mids[1:2]) * length(data)
#Draw normal curve
lines(xfit, yfit, col = "black", lwd = 2)
输出:
期望:
是否可以在没有任何外部库的情况下使用+/-误差值将误差线添加到直方图中?
答案 0 :(得分:1)
您应该可以使用arrows()
函数绘制它们:
## Create a histogram from random data
> hist(sample(runif(100)))
> arrows(x0 = 0.15, y0 = 11, x1 = 0.15, y1 = 13, code = 3, length = 0.05, angle = 90)
x0
和x1
指定x起点和终点坐标(对于垂直直线,请保持不变)
y0
和y1
指定y的开始和结束坐标,例如绘制线的长度。
code = 3
告诉R画一个双面的“箭头”,angle = 90
使得“箭头”成为一条扁平线,而length = 0.05
指定误差线应该有多宽。
有关更多详细信息,请参见?arrows
。