Beginners Cookbook for Interactive Visualization in R with highcharter

[This article was first published on r-bloggers on Programming with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


This Post is purely aimed at helping beginners with cookbook-style code for Interactive Visualizations using highcharter package in R.

About highcharter

highcharter by Joshua Kunst R package is a wrapper for the ‘Highcharts’ library including shortcut functions to plot R objects. ‘Highcharts’ is a charting library offering numerous chart types with a simple configuration syntax.


library(tidyverse) #data Manipulation
library(highcharter) #interactive visualization

Reading Input dataset and Summary of it

survey18 <- read_csv("~/so_survey_results_public.csv")


highcharter lets you plot using two different functions:

  • highchart()
  • hchart()


This function creates a Highchart chart using htmlwidgets. The widget can be rendered on HTML pages generated from R Markdown, Shiny, or other applications. If you are familiar with ggplot2 package, this function is very similar to ggplot() of the package where a base ggplot object is defined upon which further geometric layers can be added.

Similarly, Once the highchart() function is defined further highchart elements can be added on top of it.


hchart() is a generic function to draw different charts on the fly. The resulting chart is a highchart object so you can keep modifying with the implmented API. If you are familiar with ggplot2, this function is similar to qplot() of it.

Let us begin our Interactive Visualization journey with the easy plots.

Icons Plot

survey18 %>% 
  select(Gender) %>% 
  filter(! %>% 
  filter(Gender %in% c('Male','Female')) %>% 
  count(Gender) %>% 
  mutate(perc = round((n /sum(n))*100)) -> gender_icons
  hciconarray(c('Female','Male'),gender_icons$perc,icons = c('male','female'))


hchart - column

Barplot is useful when you have comparable Categorical variables (factors). Let us look at what Years of Coding experience the respondents have got.

survey18 %>% 
  count(YearsCoding) %>% 
  hchart('column', hcaes(x = 'YearsCoding', y = 'n'))

As you can see in the above code, the chart type here is column that makes a vertical bar plot. Aesthetics are given using hcaes() (similar to aes() of ggplot2).

hchart - bar

Let us look at what people are hoping for five years.

survey18 %>% 
  count(HopeFiveYears) %>% 
  hchart('column', hcaes(x = 'HopeFiveYears', y = 'n'))

while the same column plot as above does the job, it can seen the large axis label have to be rotated and also cut - which may not be something fine always. Hence we will rotate the plot to make it a horizontal bar plot so large axis label can be accomodated.

survey18 %>% 
  count(HopeFiveYears) %>% 
  hchart('bar', hcaes(x = 'HopeFiveYears', y = 'n'))

The chart type here is bar.

Grouped Bar

Let us try to add one more dimension to our existing bar, by seeing how this hope for next five years varies based on their employement type.

survey18 %>% 
  count(Employment, HopeFiveYears) %>% 
  hchart('bar', hcaes(x = 'HopeFiveYears', y = 'n', group = 'Employment'))

Now, this chart is as same as the above one except with the addition of grouped by Employement type.

But the grouping can be flipped to see the story from a different lens.

survey18 %>% 
  count(Employment, HopeFiveYears) %>% 
  hchart('bar', hcaes(x = 'Employment', y = 'n', group = 'HopeFiveYears'))

Line & Area


Line is particularly prefered when you have Time variable in x-axis but considering we don’t have any Time variable in the given dataset, we can chart out categorical varaibles using Line graph.

survey18 %>% 
  count(YearsCoding) %>% 
  hchart('line', hcaes(x = 'YearsCoding', y = 'n'))

Grouped Line

survey18 %>% 
  count(YearsCoding, Employment) %>% 
  hchart('line', hcaes(x = 'YearsCoding', y = 'n', group = "Employment"))


Line with Polynomial Interpolation.

survey18 %>% 
  count(YearsCoding) %>% 
  hchart('spline', hcaes(x = 'YearsCoding', y = 'n'))

Grouped Spline

survey18 %>% 
  count(YearsCoding, Employment) %>% 
  hchart('spline', hcaes(x = 'YearsCoding', y = 'n', group = "Employment"))


survey18 %>% 
  count(YearsCoding) %>% 
  hchart('area', hcaes(x = 'YearsCoding', y = 'n'))

Grouped Area

survey18 %>% 
  count(YearsCoding, Employment) %>% 
  hchart('area', hcaes(x = 'YearsCoding', y = 'n', group = "Employment"))

Area Spline

survey18 %>% 
  count(YearsCoding) %>% 
  hchart('areaspline', hcaes(x = 'YearsCoding', y = 'n'))

Grouped Area Spline

survey18 %>% 
  count(YearsCoding, Employment) %>% 
  hchart('areaspline', hcaes(x = 'YearsCoding', y = 'n', group = "Employment"))

As you can see in all the above plots, it’s just the chart type changes and yet for the same data hchart() function is capable of managing to plot a different chart with the same data which makes this package really an easy way to make plots.

Scatter Plot

survey18 %>% 
         Gender %in% c('Male','Female')) %>% 
  filter(Country %in% 'United Kingdom') %>% 
         ConvertedSalary > 0) %>% 
  select(Gender, Country, Age, ConvertedSalary) %>% 
  mutate(age_grp = parse_number(Age)) %>%

To leave a comment for the author, please follow the link and comment on their blog: r-bloggers on Programming with R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)