Skip to content

Instantly share code, notes, and snippets.

@kbroman
Created November 1, 2016 01:48
Show Gist options
  • Select an option

  • Save kbroman/eca4b3db87796141978cb15bc05d3d2e to your computer and use it in GitHub Desktop.

Select an option

Save kbroman/eca4b3db87796141978cb15bc05d3d2e to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 2 in line 1.
# Halloween trick-or-treaters 2016-10-31
time,number
5:59,1
6:14,10
6:29,1
6:40,2
6:42,1
6:43,2
7:04,2
7:16,1
7:19,4
7:25,1
# Halloween 2016 counts figure
# Karl Broman
# http://kbroman.org
# the data: time and kid count
x <- read.csv("halloween2016.csv", comment.char="#")
colnames(x) <- c("time", "n.kids")
# cumulative numbers of kids
csum <- cumsum(x$n.kids)
# times as times and numbers
ti <- strptime(x$time, "%H:%M")
ti.n <- as.numeric(ti)
# x-axis labels
xax.ch <- paste(c(4,4,5,5,6,6,7,7,8), sprintf("%02d", c(0,30,0,30,0,30,0,30,0)), sep=":")
xax.t <- strptime(xax.ch, "%H:%M")
xax.n <- as.numeric(xax.t)
# y-axis locations
yax <- seq(0, 28, by=2)
# make empty plot
plot(0,0,type="n", xlab="", ylab="", ylim=c(0, max(csum)+1.5), yaxs="i",
xlim=range(xax.n), xaxt="n", las=1, yaxt="n", xaxs="i",
main="Halloween 2016", cex.main=1.5)
# central part made gray
u <- par("usr")
rect(u[1], u[3], u[2], u[4], col="gray90")
# axis labels
axis(side=1, at=xax.n, xax.ch, tick=FALSE, line=-0.8)
axis(side=2, at=yax, las=1, tick=FALSE, line=-0.6)
# grid lines
abline(v=xax.n, col="white")
abline(h=yax, col="white")
# ensure the black border is there
abline(v=u[1:2], h=u[3:4])
# at first and last endpoints
ti.n <- c(u[1], ti.n, u[2])
csum <- c(0, csum, max(csum))
# data line segments + points
for(i in 1:(length(ti.n)-1)) {
segments(ti.n[i], csum[i], ti.n[i+1], csum[i], lwd=2, col="blue3", lend=1, ljoin=1)
if(i>1) points(ti.n[i], csum[i], pch=16, cex=0.8, col="blue3")
}
# axis titles
mtext(side=2, "Cumulative number of kids", line=2, cex=1.3)
mtext(side=1, "Time", line=2, cex=1.3)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment