Skip to content

Instantly share code, notes, and snippets.

@reinholdsson
Created June 22, 2013 20:28
Show Gist options
  • Save reinholdsson/5842467 to your computer and use it in GitHub Desktop.
Save reinholdsson/5842467 to your computer and use it in GitHub Desktop.
rCharts: Highcharts - Stacked and grouped column
require(rCharts)
df <- data.frame(
name = c("John", "John", "Joe", "Joe", "Jane", "Jane", "Janet", "Janet"),
data = sample(1:10, 8, replace = T),
stack = c(rep("male", 4), rep("female", 4))
)
a <- rCharts:::Highcharts$new()
a$chart(type = "column")
a$plotOptions(column = list(stacking = "normal"))
a$xAxis(categories = c("Apples", "Oranges"))
# a$series(name = "John", data = c(5, 3), stack = "male")
# a$series(name = "Joe", data = c(3, 4), stack = "male")
# a$series(name = "Jane", data = c(2, 5), stack = "female")
# a$series(name = "Janet", data = c(3, 1), stack = "female")
invisible(sapply(unique(df$name), function(name){
d <- df[df$name == name, ]
a$series(name = name, data = d$data, stack = d$stack[[1]])
}))
a
<!doctype HTML>
<html>
<head>
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
<link rel='stylesheet' href="http://twitter.github.io/bootstrap/assets/js/google-code-prettify/prettify.css">
<script src='http://code.jquery.com/jquery-1.9.1.min.js' type='text/javascript'></script>
<script src='http://code.highcharts.com/highcharts.js' type='text/javascript'></script>
<script src='http://code.highcharts.com/highcharts-more.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 800px;
height: 400px;
}
body {
margin-top: 60px;
}
</style>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span9'>
<div id='chart66537d4a930' class='rChart nvd3Plot highcharts'></div>
<br/>
<pre><code class='r'>require(rCharts)
df &lt;- data.frame(
name = c(&quot;John&quot;, &quot;John&quot;, &quot;Joe&quot;, &quot;Joe&quot;, &quot;Jane&quot;, &quot;Jane&quot;, &quot;Janet&quot;, &quot;Janet&quot;),
data = sample(1:10, 8, replace = T),
stack = c(rep(&quot;male&quot;, 4), rep(&quot;female&quot;, 4))
)
a &lt;- rCharts:::Highcharts$new()
a$chart(type = &quot;column&quot;)
a$plotOptions(column = list(stacking = &quot;normal&quot;))
a$xAxis(categories = c(&quot;Apples&quot;, &quot;Oranges&quot;))
# a$series(name = &quot;John&quot;, data = c(5, 3), stack = &quot;male&quot;)
# a$series(name = &quot;Joe&quot;, data = c(3, 4), stack = &quot;male&quot;)
# a$series(name = &quot;Jane&quot;, data = c(2, 5), stack = &quot;female&quot;)
# a$series(name = &quot;Janet&quot;, data = c(3, 1), stack = &quot;female&quot;)
invisible(sapply(unique(df$name), function(name){
d &lt;- df[df$name == name, ]
a$series(name = name, data = d$data, stack = d$stack[[1]])
}))
a
</code></pre>
</div>
</div>
</div>
<script type='text/javascript'>
(function($){
$(function () {
var chart = new Highcharts.Chart({
"dom": "chart66537d4a930",
"width": 600,
"height": 400,
"credits": {
"href": null,
"text": null
},
"title": {
"text": null
},
"yAxis": {
"title": {
"text": null
}
},
"chart": {
"type": "column",
"renderTo": "chart66537d4a930"
},
"plotOptions": {
"column": {
"stacking": "normal"
}
},
"xAxis": {
"categories": [ "Apples", "Oranges" ]
},
"series": [
{
"name": "John",
"data": [ 5, 4 ],
"stack": "male"
},
{
"name": "Joe",
"data": [ 9, 6 ],
"stack": "male"
},
{
"name": "Jane",
"data": [ 7, 9 ],
"stack": "female"
},
{
"name": "Janet",
"data": [ 6, 3 ],
"stack": "female"
}
],
"id": "chart66537d4a930"
});
});
})(jQuery);
</script>
</body>
<!-- Google Prettify -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
<script
src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'>
</script>
<script>
var pres = document.getElementsByTagName("pre");
for (var i=0; i < pres.length; ++i) {
pres[i].className = "prettyprint linenums";
}
prettyPrint();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment