library(ggplot2)
library(ggrepel)

d <- read.csv("countriesSporcle.csv")

d$GDP <- d$Nominal.GDP..millions.USD.

d <- d[complete.cases(d$GDP),]

d$Percentage <- as.numeric(gsub("%","",d$Percentage.Answered))

fit <- lm(d$Percentage ~ log10(d$GDP))

d$Outlier <- abs(fit$residuals)>10

g <- ggplot(d,aes(x=d$GDP, 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("GDP (Nominal, millions USD)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs GDP (Nominal)")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-GDP-points.png")

g <- ggplot(d,aes(x=d$GDP, 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$GDP>1e7|d$Percentage<45,as.character(d$Country),"")
))
g <- g + xlab("GDP (Nominal, millions USD)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs GDP (Nominal)")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-GDP-some-labels.png")

g <- ggplot(d,aes(x=d$GDP, 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("GDP (Nominal, millions USD)")
g <- g + ylab("Percentage of Users who Guessed Country on Sporcle Quiz")
g <- g + ggtitle("Sporcle Country Recall vs GDP (Nominal)")
g <- g + guides(color=guide_legend("Continent"))
g
ggsave(g,file="Sporcle-GDP-text.png")