Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# get the data as per
# convert to data.frame, attach and sort region
df <-
# get rid of NAs
df <- df[ !,]
df$region <- factor(df$region,
levels=c("Africa","Oceania","Asia","South America","North America", "Europe"))
# if you need to
# install.packages("GISTools")
# get some colours, add alpha
cols <- brewer.pal(6, "Dark2")
acols <- add.alpha(cols, alpha=0.3)
# create "jitter" and plot the basic plot
rnds <- runif(nrow(df), min=-0.2, max=0.2)
# make some room!
# plot
plot(student_ratio, as.numeric(region)+rnds, col=acols[df$region], pch=16,
xaxt="n", yaxt="n", xlab="Student to teacher ratio", ylab="",
xlim=c(0,90), cex=1.2, bty="n")
# Add some axes
axis(side=1, at=c(0,25,50,75), cex.axis=0.8, lwd=0)
axis(side=2, at=1:6, labels=levels(df$region), las=2, lwd=0, cex.axis=1, font=3)
# get the mean points and plot them
mns <- aggregate(df$student_ratio, by=list(region=df$region), mean)
points(mns$x, 1:6, pch=16, cex=2, col=cols)
# add the global mean line
globalMean <- mean(df$student_ratio)
lines(c(globalMean, globalMean), y=c(0,10))
text(globalMean+1, 6.3, "Global Average", pos=4, cex=0.8)
# add lines from means to global mean
for (i in 1:6) {
lines(c(mns$x[i],globalMean), c(i,i), col=cols[i], lwd=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment