Joshua L. Britt
Let's generate some data, and create a histogram
s = sample(50:500, 100, replace = T)
h = hist(s, xlim = c(0, max(s) + 10), col = "Steelblue3", right = F)
h
## $breaks
## [1] 50 100 150 200 250 300 350 400 450 500
##
## $counts
## [1] 13 11 15 9 6 11 8 14 13
##
## $intensities
## [1] 0.0026 0.0022 0.0030 0.0018 0.0012 0.0022 0.0016 0.0028 0.0026
##
## $density
## [1] 0.0026 0.0022 0.0030 0.0018 0.0012 0.0022 0.0016 0.0028 0.0026
##
## $mids
## [1] 75 125 175 225 275 325 375 425 475
##
## $xname
## [1] "s"
##
## $equidist
## [1] TRUE
##
## attr(,"class")
## [1] "histogram"
Now we will create our x,y coordinates from the counts and mids variables.
mp = c(min(h$mids) - (h$mids[2] - h$mids[1]), h$mids, max(h$mids) + (h$mids[2] -
h$mids[1]))
mp
## [1] 25 75 125 175 225 275 325 375 425 475 525
freq = c(0, h$counts, 0)
freq
## [1] 0 13 11 15 9 6 11 8 14 13 0
Now we add our lines
h = hist(s, xlim = c(0, max(s) + 10), col = "Steelblue3", right = F)
lines(mp, freq, type = "b", pch = 20, col = "red", lwd = 3)
Now we inspect our bins to see if our hand calculation is correct.
bins = table(cut(s, breaks = seq(from = min(h$breaks), to = max(h$breaks), by = h$breaks[2] -
h$breaks[1]), right = F))
bins
##
## [50,100) [100,150) [150,200) [200,250) [250,300) [300,350) [350,400)
## 13 11 15 9 6 11 8
## [400,450) [450,500)
## 14 13
Let's now create the ogive… ucl,freq
ucl = seq(from = min(h$breaks), to = max(h$breaks), by = h$breaks[2] - h$breaks[1])
ucl = c(0, ucl[-1])
ucl
## [1] 0 100 150 200 250 300 350 400 450 500
cf = c(0, cumsum(h$counts))
cf
## [1] 0 13 24 39 48 54 65 73 87 100
Now Plot the Ogive
par(bg = "gray90")
plot(ucl, cf, type = "b", col = "blue", pch = 20)