The example at hand contains code from the Plotly package. A dataset that contains variables for country name, country code and number of beneficiaries (e.g. of a charity) is visualized on a world map.
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
)
p # print map