Skip to content

Instantly share code, notes, and snippets.

@mhoehle
Last active May 9, 2019 21:48
Show Gist options
  • Select an option

  • Save mhoehle/d05ac3ae76a3d90440870a004f0b943c to your computer and use it in GitHub Desktop.

Select an option

Save mhoehle/d05ac3ae76a3d90440870a004f0b943c to your computer and use it in GitHub Desktop.
##...
##run blog post code available as https://raw.githubusercontent.com/hoehleatsu/hoehleatsu.github.io/master/_source/2019-05-06-wcamining.Rmd
##...
######################################################################
## Determine proportion of female cubers
######################################################################
female_prop <- persons %>% group_by(id) %>% group_by(countryId) %>%
summarise(n_total=n(), n_male=sum(gender=="m"), n_female=sum(gender=="f")) %>%
mutate(`frac_female (in %)`= n_female/n_total*100) %>% ungroup %>%
collect() %>%
filter(n_total >= 10)
##Join with the country information
female_prop <- inner_join(female_prop, countries %>% collect(), by=c("countryId"="id")) %>% mutate(prop = `frac_female (in %)`)
## Use rworldmap documented, e.g., in https://journal.r-project.org/archive/2011-1/RJournal_2011-1_South.pdf
library("rworldmap")
library("RColorBrewer")
##Join data and the map
female_map <- joinCountryData2Map( female_prop, joinCode = "ISO2", nameJoinColumn = "iso2")
library(classInt)
#getting class intervals
classInt <- classIntervals( female_map[["prop"]],n=7, style = "quantile")
catMethod = classInt[["brks"]]
#getting colours
##colourPalette <- brewer.pal(7,'Purples')
colourPalette <- brewer.pal(7,'BuPu')
######################################################################
## Plot it
######################################################################
png(file="map.png", width=2000, height=1000, res=200)#, bg = "transparent")
##par(mai=c(0.4, 0.3,0.15,0.15),xaxs="i",yaxs="i")
par(mar=c(0,2,4,2),xaxs="i",yaxs="i")
mapParams <- mapCountryData(female_map,nameColumnToPlot="prop", catMethod = catMethod,colourPalette=colourPalette, addLegend=FALSE, mapTitle="Percentage of female cubers per country\n(Source: WCA results database)")
do.call(addMapLegend,c(mapParams, legendLabels="all",legendWidth=0.5,legendIntervals="data", digits=0, legendMar = 2))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment