Skip to content

Visualising Tourism Data using R with googleVis package

Inspired by Mages’s post on Accessing and plotting World bank data with R (using googleVis package), I created one visualising tourism receipts and international tourist  arrivals of various countries since 1995. The data used are from the World Bank’s country indicators.

To see the motion chart, double click a picture below.

Tourism googleVis




getWorldBankData <- function(id='SP.POP.TOTL', date='1960:2010',
 url <- paste("", id,
 "?date=", date, "&format=json&per_page=",,

 wbData <- fromJSON(url)[[2]]

 wbData = data.frame(
 year = as.numeric(sapply(wbData, "[[", "date")),
 value = as.numeric(sapply(wbData, function(x)
 ifelse(is.null(x[["value"]]),NA, x[["value"]]))), = sapply(wbData, function(x) x[["country"]]['value']), = sapply(wbData, function(x) x[["country"]]['id'])

 names(wbData)[2] <- value


getWorldBankCountries <- function(){
 wbCountries <-
 wbCountries <- data.frame(t(sapply(wbCountries[[2]], unlist)))
 wbCountries$longitude <- as.numeric(wbCountries$longitude)
 wbCountries$latitude <- as.numeric(wbCountries$latitude)
 levels(wbCountries$region.value) <- gsub(" \\(all income levels\\)",
 "", levels(wbCountries$region.value))

## Create a string 1960:this year, e.g. 1960:2011
years <- paste("1960:", format(Sys.Date(), "%Y"), sep="")

## International Tourism Arrivals
inter.tourist.arrivals<- getWorldBankData(id='ST.INT.ARVL',
 date=years, value="International tourism, number of arrivals")

## International Tourism Receipts
tourism.receipts <- getWorldBankData(id='ST.INT.RCPT.CD', date=years,
 value="International tourism, receipts (current US$)")

## Population
population <- getWorldBankData(id='SP.POP.TOTL', date=years,

## GDP per capita (current US$)
GDP.per.capita <- getWorldBankData(id='NY.GDP.PCAP.CD',

## Merge data sets
wbData <- merge(tourism.receipts, inter.tourist.arrivals)
wbData <- merge(wbData, population)
wbData <- merge(wbData, GDP.per.capita)

## Get country mappings
wbCountries <- getWorldBankCountries()

## Add regional information
wbData <- merge(wbData, wbCountries[c("iso2Code", "region.value",
 by.x="", by.y="iso2Code")

## Filter out the aggregates and country id column
subData <- subset(wbData, !region.value %in% "Aggregates" , select=

## Create a motion chart
M <- gvisMotionChart(subData, idvar="", timevar="year",
 options=list(width=700, height=600))

## Display the chart in your browser

# save as a file
print(M, file="myGoogleVisChart.html")

15 Comments Post a comment
  1. MK #

    Nice post.

    You can copy paste the html code in your page source to directly display the motion chart on the webpage.

    I did a similar thing for one my posts.

    December 21, 2012
  2. RIck #

    ## Add regional information THIS DID NOT WORK
    wbData <- merge(wbData, wbCountries[c 1=""region.value",
    " 2=""incomeLevel.value")" language="("iso2Code","][/c],
    by.x="", by.y="iso2Code")

    December 22, 2012
    • how?

      December 22, 2012
      • RIck #

        > wbData <- merge(wbData, wbCountries[c 1=""region.value",
        Error: unexpected numeric constant in "wbData ” 2=””incomeLevel.value”)” language=”(“iso2Code”,”][/c],
        Error: unexpected string constant in “” 2=””incomeLevel.value””
        > by.x=””, by.y=”iso2Code”)
        Error: unexpected ‘,’ in ” by.x=””,”

        December 23, 2012
      • Hi, thanks for the feedback.
        I just run the code again but no error found.
        Actually I didn’t change that line from the original code below.
        Could you try reproduce the chart in the link above?

        December 23, 2012
      • HRISHIKESH Vinod [Staff/Faculty [A&S]] #

        yes it worked that way thanks sorry to have bothered you. It is a great piece of software you have made available to the world. Many thanks. There are other variables I am interested than tourism

        How do I get more info about what is available at the world bank dataset? Feel free to ignore this question. I should simply search the bank website. But I could not get much headway doing that

        December 24, 2012
    • You’re welcome.

      I actually put the link in the introduction text in the “country indicator”.

      Here is the link

      Replacing the code at the end of the URL of the indicator you want in the line 42 and 46 in the code.

      December 24, 2012
      • HRISHIKESH Vinod [Staff/Faculty [A&S]] #

        Thanks the link you provided does have the data, but not the id code For example, id=’SP.POP.TOTL’ where do they have a list of these id codes?

        December 24, 2012
  3. I had the same problem….the line was:
    > wbData <- merge(wbData, wbCountries[c 1=""region.value"," 2=""incomeLevel.value")" language="("iso2Code","][/c],
    Error: unexpected numeric constant in "wbData by.x=””, by.y=”iso2Code”)
    Error: unexpected ‘,’ in ” by.x=””,”

    December 22, 2012
  4. M.Adams #

    Hi Pairach, Thats a cool chart. I am a management guy and started using googleVis for dashboard reporting recently. I am a newbie to R. I am sorry if I sound ignorant but can we share these html links via sharepoint for another person to explore?

    December 22, 2012
    • Thanks a lot for kind words.
      Yes, you can share it.

      December 23, 2012
  5. Javier #

    There is a difference between the code in this page and that in r-bloggers which has the following commands in lines 66-69 that produce the error mentioned by Erin Hodgess:

    ## Add regional information
    wbData <- merge(wbData, wbCountries[c 1=""region.value"," 2=""incomeLevel.value")" language="("iso2Code","][/c],
    by.x="", by.y="iso2Code")

    However the commands in this page are ok:
    ## Add regional information
    wbData <- merge(wbData, wbCountries[c("iso2Code", "region.value",
    by.x="", by.y="iso2Code")

    December 24, 2012
  6. Just wanted to state I’m lucky I happened in your site!

    July 28, 2013

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: