Highcharts is a JavaScript charting library based on SVG and VML rendering by Highsoft Solutions AS. It is not free for commercial use, so make sure you have a valid license. For more information, see http://www.highcharts.com/.
To initialize a new chart object one simply write
obj <- Highcharts$new()
Hint: An alternative is to use the custom wrapper function hPlot
.
The Highcharts API Reference has an extensive documentation of all methods in the Highcharts javascript library, those implemented in rCharts
here follows with examples.
We set the type
and height
with the chart
method.
obj$chart(type = "column", height = 300)
As default, line
is used as the chart type.
obj$series(data = 1:3)
It is possible to add another data series by re-using series
on the same object. While we in the above example only provided the y-coordinates we could also specify the x-coordinates (otherwise starting from 0), as below:
obj$series(data = list(c(0,2), c(1,1), c(2,3)), name = "foo")
We also named it by using the name
argument.
The third alternative to add data is perhaps the most useful for more advanced charts, for example when using the tooltip
method as one might be interested to show additional data in the chart, other than x- and y-values.
obj$series(data = list(
list(x = 0, y = 0, foo = "a"),
list(x = 1, y = 1, foo = "b"),
list(x = 2, y = 3, foo = "c")
), type = "spline")
As noticed, the chart type
may as well be included as an attribute, which makes it possible to combine several chart types within the same chart. The series are layered on each other in the same order as they are added, therefore the spline
will be shown on top of the columns
.
It is also possible to add all series at once, as a list:
obj$series(list(
list(
data = 1:3
),
list(
data = list(c(0,2), c(1,1), c(2,3)), name = "foo"
),
list(
data = list(
list(x = 0, y = 0, foo = "a"),
list(x = 1, y = 1, foo = "b"),
list(x = 2, y = 3, foo = "c")
), type = "spline"
)
), replace = T)
replace = T
is also included to remove all previously created series. The replace
argument is available in all Highcharts
methods as to decide whether to clear or keep all previously set parameters (except for in series
, its default is always set to TRUE
).
obj$xAxis(categories = c("apple", "orange", "pear"))
obj$yAxis(title = list(text = "Number"))
obj$colors(
'rgba(223, 83, 83, .75)',
'rgba(60, 179, 113, .75)',
'rgba(238, 130, 238, .75)'
)
obj$credits(text = "Created with rCharts", href = "http://rcharts.io")
obj$tooltip(useHTML = T, formatter = "#! function() { return this.x + ', ' + this.y; } !#")
obj$labels(items = list(
list(
html = "An interactive example of Highcharts in rCharts",
style = list(left = "20px", top = "20px")
)
))
The items
argument takes an array of items, and that is why we use list
twice.
obj$legend(
align = 'right',
verticalAlign = 'middle',
layout = 'vertical'
)
obj$plotOptions(
column = list(stacking = "normal"),
spline = list(dashStyle = "Dash")
)
obj$title(text = "Chart Title")
obj$subtitle(text = "This is a subtitle")
Although we have covered the most commonly used methods, there are still some that we have not described (that might be of interested to more advanced users):
And custom methods (only available for R users):
For more information on how to use these, see the Highcharts API documentation page.