Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Philosophy
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’ http://www.highcharts.com/ is a charting library offering numerous chart types with a simple configuration syntax.
Libraries
library(tidyverse) #data Manipulation library(highcharter) #interactive visualization
Reading Input dataset and Summary of it
survey18 <- read_csv("~/so_survey_results_public.csv") #glimpse(survey18)
API
highcharter
lets you plot using two different functions:
- highchart()
- hchart()
highchart()
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()
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(!is.na(Gender)) %>% 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'))
Bar
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
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"))
Spline
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"))
Area
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 %>% filter(!is.na(Gender), Gender %in% c('Male','Female')) %>% filter(Country %in% 'United Kingdom') %>% filter(!is.na(Age), !is.na(ConvertedSalary), ConvertedSalary > 0) %>% select(Gender, Country, Age, ConvertedSalary) %>% mutate(age_grp = parse_number(Age)) %>% hchart('scatter',hcaes('ConvertedSalary',"age_grp"))
R-bloggers.com 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.