Video link https://youtu.be/n3ruvKHWuhE
We will create a barchart in R, customise it and then save it as a static image so that you can use it in your word document reports.
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
install.packages(highcharter)
install.packages(dplyr)
install.packages(gapminder)
Once you have your packages installed then we will load the packages in the r environment using the following commands
# 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.
# Let us view the data
head(gapminder)
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)%>%
head(10)
ds
We will use the country and the pop data fields to create our bar chart. Vertical column bar charts can be created by type = “column”
pl <- hchart(object =ds, type = "column", hcaes(x = country, y = pop), color ='red')
pl
Horizontal column bar charts can be created by type = “bar”
pl <- hchart(object =ds, type = "bar", hcaes(x = country, y = pop), color ='red')
pl
This is a different syntax which can be used
pl <- highchart() %>%
hc_chart(type = "column") %>%
hc_add_series(name = "population" , data = ds$pop , color ="green" )%>%
hc_xAxis(categories = ds$country)%>%
hc_xAxis(title = list(text = "Countries"))%>%
hc_yAxis(title = list(text = "Population in million"))%>%
hc_title(text= "Column chart using highcharter") %>%
hc_subtitle(text= "Top 10 countries by population") %>%
hc_caption(text= "Based on year 2007 population")%>%
hc_add_theme(hc_theme_bloom())%>%
hc_legend(enabled= FALSE)
pl
Here is a fancy theme hc_theme_chalk There are a number of themes which you can try.
head(mtcars)
pl3 <- highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Using Highcharter for creating a bar chart") %>%
hc_subtitle(text = "techanswers88"
,style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
hc_add_series(name = "Miles Per Gallon" , data = mtcars$mpg , color ="green" )%>%
hc_add_series(name = 'Horse Power' , data = mtcars$hp , color = "blue" )%>%
hc_xAxis(categories = rownames(mtcars)
, title = list(text = "Car Models"
, style = list(fontSize = "14px"
, fontWeight = "bold"
, color = "blue"))) %>%
hc_yAxis(title = list(text = "Values"
,style = list(fontSize = "14px"
, fontWeight = "bold"
, color = "red"))) %>%
hc_add_theme(hc_theme_chalk())
pl3
As highcharter charts are rendered slowly on screen so give it a delay before saving the chart. In our example we have used delay = 4 so that the chart gets saved after 4 seconds when it start rendering. If your chart is complicated and takes more time to render and your saved image is not complete then increase the delay eg. delay = 10
Remember that it will save the chart which was created last. If you are creating 10 charts then you can use the same command after
## 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 = pl3, file = "D:\\tmp\\myChart.html")
webshot::webshot(url = "D:\\tmp\\myChart.html" , file ="D:\\tmp\\BarChart.png", delay = 4 )