-
-
Save k3yavi/9444ce82e6a4a4adc3d2b7241c0a3d4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get the data as per https://www.cedricscherer.com/2019/05/17/the-evolution-of-a-ggplot-ep.-1/#code | |
library(tidyverse) | |
devtools::source_gist("https://gist.github.com/Z3tt/301bb0c7e3565111770121af2bd60c11") | |
# convert to data.frame, attach and sort region | |
df <- as.data.frame(df_ratios) | |
attach(df) | |
# get rid of NAs | |
df <- df[ ! is.na(student_ratio),] | |
df$region <- factor(df$region, | |
levels=c("Africa","Oceania","Asia","South America","North America", "Europe")) | |
# if you need to | |
# install.packages("GISTools") | |
library(GISTools) | |
# get some colours, add alpha | |
library(RColorBrewer) | |
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! | |
par(mar=c(5,8,2,1)) | |
# plot | |
attach(df) | |
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