Highcharter R Package Essentials for Easy Interactive Graphs

Highchart Interactive World Map in R

This article describes how to create an interactive World map in R using the highcharter R package. You will also learn how to create a choropleth map, in which areas are patterned in proportion to a given variable values being displayed on the map, such as population life expectancy or density.



# Load required R packages

# Set highcharter options
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))

Data preparation

Here, we’ll create world map colored according to the value of life expectancy at birth in 2015. The data is retrieved from the WHO (World Health Organozation) data base using the WHO R package.

# Retrieve life expectancy data for the year 2015
life.exp <- get_data("WHOSIS_000001")             
life.exp <- life.exp %>%
  filter(year == 2015 & sex == "Both sexes") %>%
  select(country, value)  
## # A tibble: 191 x 2
##   country                          value
##   <chr>                            <dbl>
## 1 Austria                           81.4
## 2 Benin                             60.7
## 3 Bahrain                           78.8
## 4 Bolivia (Plurinational State of)  71.2
## 5 Switzerland                       83  
## 6 Democratic Republic of the Congo  60.1
## # … with 185 more rows
# Load the world Map data
data(worldgeojson, package = "highcharter")

Create the choropleth map

hc <- highchart() %>%
    worldgeojson, life.exp, value = "value", joinBy = c('name','country'),
    name = "LifeExpectancy"
    )  %>% 
  hc_colorAxis(stops = color_stops()) %>% 
  hc_title(text = "World Map") %>% 
  hc_subtitle(text = "Life Expectancy in 2015")

Version: Français

Highchart Interactive Funnel Chart in R (Prev Lesson)
(Next Lesson) Highchart Interactive Pyramid Chart in R
Back to Highcharter R Package Essentials for Easy Interactive Graphs

No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments