This is just another wrapper for highcharts javascript library for R. The mainly motivation to this packages is get a the all the power from HCs API with no restrictions and write the plots using the pipe operator just like metricsgraphics and dygraphs.
Highcharts is very mature javascript charting library and it has a great and powerful API to get a very style of charts and highly customized (see http://www.highcharts.com/demo).
In the package there’s some partial implementation of the HC API in R like hc_title, hc_add_serie, hc_xAxis, and some shorcuts to made simples chart in R by if you want you can create your chart manually with all the requiriments what you need. That’s the package offer.
You can install the package via devtools: devtools::install_github("jbkunst/rchess").
Let’s start doing a simple column chart:
library("highcharter")
library("magrittr")
hc <- highchart(debug = TRUE) %>%
hc_title(text = "A nice chart") %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) %>%
hc_add_serie(data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2,
26.5, 23.3, 18.3, 13.9, 9.6))
hc
With the implemented API you can modify the previous chart in a easy way. Let’s do some changes:
hc <- hc %>%
hc_title(style = list(color = "red")) %>%
hc_subtitle(text = "I want to add a subtitle too with style",
style = list(color = "#B71C1C", fontWeight = "bold")) %>%
hc_add_serie(name = "A another data", type = "line", color = "#1FA67A",
dataLabels = list(align = "center", enabled = TRUE),
data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2,
17.0, 16.6, 14.2, 10.3, 6.6, 4.8))
hc
And finally:
hc <- hc %>%
hc_tooltip(crosshairs = TRUE, shared = TRUE) %>%
hc_yAxis(minorGridLineWidth = 0, gridLineWidth = 0,
plotBands = list(
list(from = 10, to = 20, color = "rgba(68, 170, 213, 0.1)",
label = list(text = "A low band")),
list(from = 20, to = 25, color = "rgba(0, 0, 0, 0.1)",
label = list(text = "A medium band")),
list(from = 25, to = 30, color = "rgba(68, 170, 213, 0.1)",
label = list(text = "A high band"))
))
hc
Easy right? Well, it’s just the Highcharts API. Thanks to the HC team.
Premise: There’s not default arguments. All arguments need to be named.
Let’s use a simple plot to show how do with the differentes funcions from the package.
data(citytemp)
citytemp
## Source: local data frame [12 x 5]
##
## month tokyo new_york berlin london
## (chr) (dbl) (dbl) (dbl) (dbl)
## 1 Jan 7.0 -0.2 -0.9 3.9
## 2 Feb 6.9 0.8 0.6 4.2
## 3 Mar 9.5 5.7 3.5 5.7
## 4 Apr 14.5 11.3 8.4 8.5
## 5 May 18.2 17.0 13.5 11.9
## 6 Jun 21.5 22.0 17.0 15.2
## 7 Jul 25.2 24.8 18.6 17.0
## 8 Aug 26.5 24.1 17.9 16.6
## 9 Sep 23.3 20.1 14.3 14.2
## 10 Oct 18.3 14.1 9.0 10.3
## 11 Nov 13.9 8.6 3.9 6.6
## 12 Dec 9.6 2.5 1.0 4.8
hc <- highchart(debug = TRUE) %>%
hc_xAxis(categories = citytemp$month) %>%
hc_add_serie(name = "Tokyo", data = citytemp$tokyo) %>%
hc_add_serie(name = "London", data = citytemp$london)
hc
With hc_chart you can define general chart options.
hc %>%
hc_chart(borderColor = '#EBBA95',
borderRadius = 10,
borderWidth = 2,
backgroundColor = list(
linearGradient = c(0, 0, 500, 500),
stops = list(
list(0, 'rgb(255, 255, 255)'),
list(1, 'rgb(200, 200, 255)')
)))
Now change type to colum and add 3d effect.
hc <- hc %>%
hc_chart(type = "column",
options3d = list(enabled = TRUE, beta = 15, alpha = 15))
hc
Now remove 3deffect and add the original type
hc <- hc %>%
hc_chart(type = "line", options3d = list(enabled = FALSE))
hc
Options to add the chart’s main title and subtitle.
hc %>%
hc_title(text = "This is a title with <i>margin</i> at <b>bottom</b>",
useHTML = TRUE) %>%
hc_subtitle(text = "A detailed description",
align = "right",
style = list(color = "#2b908f", fontWeight = "bold")) %>%
hc_title(margin = 50,
align = "left",
style = list(color = "#90ed7d"))
hc
hc
hc
highchart() %>%
hc_add_serie_ts(AirPassengers)
highchart() %>%
hc_title(text = "Monthly Deaths from Lung Diseases in the UK") %>%
hc_subtitle(text = "Deaths from bronchitis, emphysema and asthma") %>%
hc_add_serie_ts(fdeaths, name = "Female") %>%
hc_add_serie_ts(mdeaths, name = "Male")
highchart() %>%
hc_add_serie_scatter(cars$speed, cars$dist)
highchart() %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg, mtcars$cyl) %>%
hc_chart(zoomType = "xy") %>%
hc_title(text = "Motor Trend Car Road Tests") %>%
hc_xAxis(title = list(text = "Weight"), minorTickInterval = "auto") %>%
hc_yAxis(title = list(text = "Miles/gallon")) %>%
hc_tooltip(headerFormat = "<b>{series.name} cylinders</b><br>",
pointFormat = "{point.x} (lb/1000), {point.y} (miles/gallon)")
hc <- highchart(debug = TRUE) %>%
hc_add_serie_scatter(mtcars$wt, mtcars$mpg, mtcars$cyl) %>%
hc_chart(zoomType = "xy") %>%
hc_title(text = "Motor Trend Car Road Tests") %>%
hc_subtitle(text = "Motor Trend Car Road Tests") %>%
hc_xAxis(title = list(text = "Weight")) %>%
hc_yAxis(title = list(text = "Miles/gallon")) %>%
hc_tooltip(headerFormat = "<b>{series.name} cylinders</b><br>",
pointFormat = "{point.x} (lb/1000), {point.y} (miles/gallon)")
hc
hc %>% hc_add_theme(hc_theme_darkunica())
hc %>% hc_add_theme(hc_theme_gridlight())
hc %>% hc_add_theme(hc_theme_sandsignika())
Insipired in https://www.amcharts.com/inspiration/chalk/
hc %>% hc_add_theme(hc_theme_chalk())
thm <- hc_theme(
colors = c('red', 'green', 'blue'),
chart = list(
backgroundColor = "#15C0DE"
),
title = list(
style = list(
color = '#333333',
fontFamily = "Erica One"
)
),
subtitle = list(
style = list(
color = '#666666',
fontFamily = "Shadows Into Light"
)
),
legend = list(
itemStyle = list(
fontFamily = 'Tangerine',
color = 'black'
),
itemHoverStyle = list(
color = 'gray'
)
)
)
hc %>% hc_add_theme(thm)