05 August 2017

Introdcution

The example at hand contains code from the Plotly package. It was created in the course of the Coursera course Developing Data Products

Dataset

A dataset that contains the following variables was used:

for country name, country code and number of beneficiaries (e.g. of a charity) is visualized on a world map.

  • country name (e.g. Austria)
  • number of beneficiaries of an international charity organization (e.g. 14.200)
  • country code (for recognition by Plotly, e.g. AUT)

Slide with R Output

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
df <-read.table("C:/users/johan/Documents/world.txt", header = TRUE) # read file
head(df) # check file
##      COUNTRY   Child CODE
## 1      India 6946000  IND
## 2      Nepal 1560000  NPL
## 3   Ethiopia 1161000  ETH
## 4    Vietnam 1025000  VNM
## 5    Burundi  954000  BDI
## 6 Bangladesh  887000  BGD
# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)

# specify map projection/options
g <- list(
    showframe = FALSE,
    showcoastlines = TRUE,
    projection = list(type = 'Mercator')
)

# start mapping
p <- plot_geo(df) %>%
    add_trace(
        z = df$Child, color = df$Child, colors = 'Blues',
        text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
    ) %>%
    colorbar(title = 'Number of beneficiaries') %>%
    layout(
        title = 'Number of beneficiaries (example)',
        geo = g
    )

Slide with Plot

p # print map