Our youtube channel

Our youtube channel has lots of videos on data visualisation in r.

Visit our youtube channel https://www.youtube.com/c/TechAnswers88

Video link https://youtu.be/-ZVnO-nNACY

Packages used in this demo

If you do not have these packages already installed on your computer then please install them using the following commands in the next threee lines

Once you have your packages installed then we will load the packages in the r environment using the following commands


#install.packages(highcharter)
#install.packages(dplyr)
#install.packages(gapminder)
  
# Packages for creating the chart
library(highcharter)

# Other supporting packages
library(dplyr)

# Sample data for our charts
library(gapminder)

Gapminder dataset will be used in this demo, so let us see what gapminder dataset is all about.

Prepare our data for the chart

We need to get the population data for the top 10 countries which had the highest population in year 2007. Following command will prepare that data for us.

ds <- gapminder%>%
       dplyr::filter(year == 2007)%>%
        dplyr::arrange(-pop)

ds

Transform your data to Boxplot data

We will use the data_to_boxplot command to transform our dataset ds to create our boxplot.

myboxplotData <- data_to_boxplot(ds
                               ,lifeExp
                               ,continent
                               ,group_var     = continent
                               ,add_outliers  = FALSE
                               ,fillColor     = c("red", "green","yellow", "pink","blue")
                                 , color        = "black"
                            
                                )

First box plot

highchart()%>%
  hc_xAxis(type ="category")%>%
  hc_add_series_list(myboxplotData)%>%
  hc_xAxis(title = list(text = "contient"))%>%
        hc_yAxis(title = list(text = "Life expectancy"))%>%
        hc_title(text= "Boxplot  using highcharter") %>% 
        hc_subtitle(text= "Life expectancy of each continent") %>% 
        hc_caption(text= "Based on year 2007 population")%>% 
        hc_legend(enabled= FALSE)

Using hc_theme_bloom

Notice that the fill colours remain the same as this information is already saved in the fillColor = c(“red”, “green”,“yellow”, “pink”,“blue”) option in the data_to_plot command


highchart()%>%
  hc_xAxis(type ="category")%>%
  hc_add_series_list(myboxplotData)%>%
  hc_xAxis(title = list(text = "contient"))%>%
        hc_yAxis(title = list(text = "Life expectancy"))%>%
        hc_title(text= "Boxplot  using highcharter") %>% 
        hc_subtitle(text= "Life expectancy of each continent") %>% 
        hc_caption(text= "Based on year 2007 population")%>% 
        hc_add_theme(hc_theme_bloom())%>%
        hc_legend(enabled= FALSE)

Using hc_theme_chalk

pl <- highchart()%>%
  hc_xAxis(type ="category")%>%
  hc_add_series_list(myboxplotData)%>%
  hc_xAxis(title = list(text = "contient"))%>%
        hc_yAxis(title = list(text = "Life expectancy"))%>%
        hc_title(text= "Boxplot  using highcharter") %>% 
        hc_subtitle(text= "Life expectancy of each continent") %>% 
        hc_caption(text= "Based on year 2007 population")%>%  
        hc_add_theme(hc_theme_chalk())%>%
        hc_legend(enabled= FALSE)
pl

Save the chart

## save as a static image to be use in your word document or presentation
library(htmlwidgets)
library(webshot)

# Use these commands for saving the charts. Change the name of the plot as needed.
htmlwidgets::saveWidget(widget = pl, file = "D:\\tmp\\myChart.html")
webshot::webshot(url = "D:\\tmp\\myChart.html" , file ="D:\\tmp\\Boxplot.png", delay = 4 )