Highcharter R Package Essentials for Easy Interactive Graphs

Highchart Interactive Treemap in R

This article describes how to create an interactive treemap in R using the highcharter R package.



Contents:

Prerequisites

# Load required R packages
library(tidyverse)
library(highcharter) 

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

Data preparation

# Load a demo data
data("mpg", package = "ggplot2")

# Summary table
summary.table <- mpg %>% 
  group_by(manufacturer) %>% 
  summarise(
    nb_cars = n(), 
    nb_model = length(unique(model))
    ) %>% 
  arrange(-nb_cars, -nb_model)
summary.table
## # A tibble: 15 x 3
##   manufacturer nb_cars nb_model
##   <chr>          <int>    <int>
## 1 dodge             37        4
## 2 toyota            34        6
## 3 volkswagen        27        4
## 4 ford              25        4
## 5 chevrolet         19        4
## 6 audi              18        3
## # … with 9 more rows

Basic treemaps

hc <- summary.table %>%
  hchart(
    "treemap", 
    hcaes(x = manufacturer, value = nb_cars, color = nb_model)
    )
hc

Change colors

hc <- summary.table %>%
  hchart(
    "treemap", 
    hcaes(x = manufacturer, value = nb_cars, color = nb_model)
    ) %>%
  hc_colorAxis(stops = color_stops(colors = viridis::inferno(10)))
hc



Version: Français

Highchart Interactive Density and Histogram Plots in R (Prev Lesson)
(Next Lesson) Highchart Interactive Area Plot 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