Highcharter - Barplots

Highcharter is one of the best interactive visualization package available in R. Barplots are the most commonly used plots in the data visualization world.

Author

Affiliation

Umesh Narayanappa

 

Published

Sept. 12, 2020

DOI

What is highcharter?

Highcharter is a R wrapper for Highcharts javascript library and its modules. Highcharts is very flexible and customizable javascript charting library and it has a great and powerful API.

Data View

We are going to use this dataset for plotting different variations of the barplots

library(tidyverse)
library(highcharter)
library(DT)

DT::datatable(mpg)

Default Bar Plot is a Horizontal plot

makers <- count(mpg, manufacturer)

hchart(
  makers,
  "bar",
  hcaes(x=manufacturer, y= n)
)
Created with Highcharts 8.1.2manufacturernaudichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen02468101214161820222426283032343638405152535

How to make it a vertical bar plot

hchart(
  makers,
  "column",
  hcaes(x=manufacturer, y= n)
)
Created with Highcharts 8.1.2manufacturernaudichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen0510152025303540

How to change the default color of the bars

hchart(
  makers,
  "column",
  hcaes(x=manufacturer, y= n),
  color = "maroon"
)
Created with Highcharts 8.1.2manufacturernaudichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen0510152025303540

How to sort the bars in the plot in a order

mpg %>%
  group_by(manufacturer) %>%
  count() %>%
  arrange(desc(n)) -> makers 

hchart(
  makers,
  "column",
  hcaes(x=manufacturer, y= n),
  color = "maroon"
)
Created with Highcharts 8.1.2manufacturerndodgetoyotavolkswagenfordchevroletaudihyundaisubarunissanhondajeeppontiacland rovermercurylincoln0510152025303540

How to Create Side by Side Bar plot

mpgman2 <- count(mpg, manufacturer, year)

hchart(
  mpgman2, 
  "bar",
  hcaes(x = manufacturer, y = n, group = year),
  color = c("maroon", "darkgrey"),
  name = c("Year 1999", "Year 2008")
  )
Created with Highcharts 8.1.2manufacturernYear 1999Year 2008audichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen0123456789101112131415161718192021222324

How to Create Side by Side Column plot

mpgman2 <- count(mpg, manufacturer, year)

hchart(
  mpgman2, 
  "column",
  hcaes(x = manufacturer, y = n, group = year),
  color = c("maroon", "darkgrey"),
  name = c("Year 1999", "Year 2008")
  )
Created with Highcharts 8.1.2manufacturernYear 1999Year 2008audichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen0510152025

How to Sort the Bars based on Categorical Attribute

mpg %>%
  group_by(manufacturer, year) %>%
  count() %>%
  arrange(desc(manufacturer)) -> makers 

hchart(
  makers, 
  "column",
  hcaes(x = manufacturer, y = n, group = year),
  color = c("maroon", "darkgrey"),
  name = c("Year 1999", "Year 2008")
  )
Created with Highcharts 8.1.2manufacturernYear 1999Year 2008volkswagentoyotasubarupontiacnissanmercurylincolnland roverjeephyundaihondaforddodgechevroletaudi0510152025

How to Create Stacked Bars plot

mpgman2 <- count(mpg, manufacturer, year)

hchart(
  mpgman2, 
  "column",
  hcaes(x = manufacturer, y = n, group = year),
  color = c("maroon", "darkgrey"),
  name = c("Year 1999", "Year 2008"),
  stacking = "normal"
  )
Created with Highcharts 8.1.2manufacturernYear 1999Year 2008audichevroletdodgefordhondahyundaijeepland roverlincolnmercurynissanpontiacsubarutoyotavolkswagen0510152025303540

Credits