### Data on the map

While surfing around the Internet I accidentally found the googleVis library for R and especially the gvisGeoMap-function which creates a map based on country data. In a table hockey scene we have a great World Ranking system which pretty much tells you who’s the top dog and also who are the active players ’cause tournaments expire in 24 months.

So I took a closer look to the googleVis/gvisGeoMap and found it out very straight-forward to use and it got great response from the players around the world.

The problem I faced with the data was that it used country codes like FIN, SWE, RUS, GBR, LAT and so on and the gvisGeoMap doesn’t recognize them so I had to write small script to recode those. **If there is a better and more efficient way to do it, please comment ’cause I believe there is.**

dynamic_players_to_map.R:

## Using the google visualization API with R
## Creates a map of table hockey players by countries. Only selects players who has a World Ranking entry (at least 1 point during 2 years)
## @author rocknrblog
## requires 'rename_countries.R'
## Version 0.2, 21.6.2011
## Feel free to use and modify
# Loads googleVis-library needed for map creation
library(googleVis)
# Reads the World Ranking file
input<- read.table("http://ithf.info/stiga/ithf/ranking/ranking.txt", sep="\t", header=TRUE, skip=1)
# Convert nation code list to a List data type (I had to do this so I could do the recoding/renaming)
nat <- as.matrix(input$Nation)
# Rename the nation-data with corresponding country names listed on the file
source('rename_countries.R')
nat <- as.factor(nat)
#Nation codes to dataframe 'df'
nation <- data.frame(x = nat)
# Frequencies of nations' players
ranking <- as.data.frame(table(nation), stringsAsFactor=FALSE)
# Create Map-dataframe of nations and frequencies
Map<- data.frame(ranking$nation, ranking$Freq)
# Name Map's attributes
names(Map)<- c("Country", "Number of Players")
# Create a map as gvisGeoMap
Geo=gvisGeoMap(Map, locationvar="Country", numvar="Number of Players", options=list(height=600, width=800, dataMode='regions'))
# Plot the map graphics file as HTML/JS
plot(Geo)

And the rename_countries.R:

## Tool script for recoding ITHF WR country codes to country names understood by googleVis
## @author Juha-Matti Santala
## Version 0.2, 21.6.2011
nat <- replace(nat, nat=="GBR", "United Kingdom")
nat <- replace(nat, nat=="FIN", "Finland")
nat <- replace(nat, nat=="RUS", "Russia")
nat <- replace(nat, nat=="AFG", "Afghanistan")
nat <- replace(nat, nat=="ALB", "Albania")
nat <- replace(nat, nat=="AUS", "Australia")
nat <- replace(nat, nat=="AUT", "Austria")
nat <- replace(nat, nat=="BLR", "Belarus")
nat <- replace(nat, nat=="CAN", "Canada")
nat <- replace(nat, nat=="CHN", "China")
nat <- replace(nat, nat=="CRO", "Croatia")
nat <- replace(nat, nat=="CZE", "Czech Republic")
nat <- replace(nat, nat=="EST", "Estonia")
nat <- replace(nat, nat=="DEN", "Denmark")
nat <- replace(nat, nat=="FRA", "France")
nat <- replace(nat, nat=="GER", "Germany")
nat <- replace(nat, nat=="HUN", "Hungary")
nat <- replace(nat, nat=="IND", "India")
nat <- replace(nat, nat=="ITA", "Italy")
nat <- replace(nat, nat=="JAP", "Japan")
nat <- replace(nat, nat=="KAZ", "Kazakhstan")
nat <- replace(nat, nat=="LAT", "Latvia")
nat <- replace(nat, nat=="LIB", "Lebanon")
nat <- replace(nat, nat=="LTU", "Lithuania")
nat <- replace(nat, nat=="NED", "Netherlands")
nat <- replace(nat, nat=="NOR", "Norway")
nat <- replace(nat, nat=="PAK", "Pakistan")
nat <- replace(nat, nat=="ROM", "Romania")
nat <- replace(nat, nat=="SRB", "Serbia")
nat <- replace(nat, nat=="SVK", "Slovakia")
nat <- replace(nat, nat=="SLO", "Slovenia")
nat <- replace(nat, nat=="KOR", "South Korea")
nat <- replace(nat, nat=="ESP", "Spain")
nat <- replace(nat, nat=="SUD", "Sudan")
nat <- replace(nat, nat=="SWE", "Sweden")
nat <- replace(nat, nat=="SUI", "Switzerland")
nat <- replace(nat, nat=="UKR", "Ukraine")
nat <- replace(nat, nat=="USA", "United States")

rename_countries.R is definitely not pretty and it looks stupid but I found out no other way to do it and I wanted to get some graphs working.

You can find the map in use here. Next thing I’m planning is some kind of a visualisation of the history and development of player counts in the world so players could see how countries have grown or shrunk during the years.