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.

Umesh Narayanappa
09-12-2020

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

Default Bar Plot is a Horizontal plot

makers <- count(mpg, manufacturer)

hchart(
  makers,
  "bar",
  hcaes(x=manufacturer, y= n)
)

How to make it a vertical bar plot

hchart(
  makers,
  "column",
  hcaes(x=manufacturer, y= n)
)

How to change the default color of the bars

hchart(
  makers,
  "column",
  hcaes(x=manufacturer, y= n),
  color = "maroon"
)

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"
)

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")
  )

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")
  )

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")
  )

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"
  )

Credits