library(ggplot2)
library(ggrepel)

d <- read.csv("countriesSporcle.csv")

d$Percentage <- as.numeric(gsub("%","",d$Percentage.Answered))

fit <- lm(d$Percentage ~ log10(d$Population2017))
d$Outlier <- abs(fit$residuals)>10

g <- ggplot(d,aes(x=d$Population2017, y=d$Percentage))
g <- g + scale_x_log10()
g <- g + geom_smooth(method=lm,se=F,color="grey")
g <- g + geom_point(size=5,aes(color=d$UN.continental.region))
g <- g + xlab("Population (July 2017)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs Population")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-points.png")

g <- ggplot(d,aes(x=d$Population2017, y=d$Percentage))
g <- g + scale_x_log10()
g <- g + geom_smooth(method=lm,se=F,color="grey")
g <- g + geom_point(size=3, aes(color=d$UN.continental.region))
g <- g + geom_text_repel(aes(
    colour=d$UN.continental.region,
    label=ifelse(d$Outlier|d$Population2017>1e9|d$Percentage<45,as.character(d$Country),"")
  ))
g <- g + xlab("Population (July 2017)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs Population")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-some-labels.png")

g <- ggplot(d,aes(x=d$Population2017, y=d$Percentage))
g <- g + scale_x_log10()
g <- g + geom_smooth(method=lm,se=F,color="grey")
g <- g + geom_text(size=3,aes(colour=d$UN.continental.region, label=d$Country))
g <- g + xlab("Population (July 2017)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs Population")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-text.png")
