R中具有正态曲线和误差线的直方图

时间:2019-01-18 20:06:39

标签: r histogram data-science graphing errorbar


    #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)

输出:

enter image description here

期望:

是否可以在没有任何外部库的情况下使用+/-误差值将误差线添加到直方图中?

1 个答案:

答案 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)

x0x1指定x起点和终点坐标(对于垂直直线,请保持不变)

y0y1指定y的开始和结束坐标,例如绘制线的长度。

code = 3告诉R画一个双面的“箭头”,angle = 90使得“箭头”成为一条扁平线,而length = 0.05指定误差线应该有多宽。

enter image description here

有关更多详细信息,请参见?arrows