Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:03
Show Gist options
  • Save timelyportfolio/882e58dad543f8f8f329 to your computer and use it in GitHub Desktop.
Save timelyportfolio/882e58dad543f8f8f329 to your computer and use it in GitHub Desktop.
rCharts + d3pie chart of US Beer
require(magrittr)
require(dplyr)
require(rCharts)
beerPie <- rCharts$new()
beerPie$setLib("http://timelyportfolio.github.io/rChartsExtra/d3pie")
# source : The Atlantic April 2014 "The State of American Beer"
# http://www.theatlantic.com/business/archive/2014/04/the-state-of-american-beer/360583/
beerData <- rbind(
c("Bud Light",5946776000),
c("Coors Light",2360430000),
c("Budweiser",2110352000),
c("Miller Light",1868866000),
c("Natural Light",1122770000),
c("Busch Light",844276000),
c("Michelob Ultra",777196700),
c("Busch",673394400),
c("Keystone Light",493993000),
c("Miller High Life",476895100),
c("Category total",23707870000)
) %>% data.frame(stringsAsFactors = F) %>% #make data.frame
set_colnames(c("label","value")) %>% #set our column names
mutate(value = as.numeric(value)) #make our values numeric
#change total row to an other row
beerData[nrow(beerData),1] <- "Other"
beerData[nrow(beerData),2] <- sum(beerData[-nrow(beerData),2])
beerPie$params$chartspec <- list(
header = list(
title = list(
text = "Domestic Beer in the U.S. 2013 | Dollar Sales"
),
subtitle = list(
text = "source: The Atlantic, April 2014"
)
)
,data = list(
content = beerData
)
,labels = list(
lines = list(style = "straight")
)
)
beerPie
<!doctype HTML>
<meta charset = 'utf-8'>
<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://getbootstrap.com/2.3.2/assets/js/google-code-prettify/prettify.css">
<link rel='stylesheet' href="http://aozora.github.io/bootplus/assets/css/docs.css">
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='http://timelyportfolio.github.io/rChartsExtra/d3pie/js/d3pie.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 500px;
}
/*
body {
margin-top: 60px;
}
*/
</style>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span8'>
<div class="bs-docs-example">
<div id='chart22d86bc65cf5' class='rChart d3pie'>
</div>
<br/>
<pre><code class='r'>require(magrittr)
require(dplyr)
require(rCharts)
beerPie &lt;- rCharts$new()
beerPie$setLib(&quot;http://timelyportfolio.github.io/rChartsExtra/d3pie&quot;)
# source : The Atlantic April 2014 &quot;The State of American Beer&quot;
# http://www.theatlantic.com/business/archive/2014/04/the-state-of-american-beer/360583/
beerData &lt;- rbind(
c(&quot;Bud Light&quot;,5946776000),
c(&quot;Coors Light&quot;,2360430000),
c(&quot;Budweiser&quot;,2110352000),
c(&quot;Miller Light&quot;,1868866000),
c(&quot;Natural Light&quot;,1122770000),
c(&quot;Busch Light&quot;,844276000),
c(&quot;Michelob Ultra&quot;,777196700),
c(&quot;Busch&quot;,673394400),
c(&quot;Keystone Light&quot;,493993000),
c(&quot;Miller High Life&quot;,476895100),
c(&quot;Category total&quot;,23707870000)
) %&gt;% data.frame(stringsAsFactors = F) %&gt;% #make data.frame
set_colnames(c(&quot;label&quot;,&quot;value&quot;)) %&gt;% #set our column names
mutate(value = as.numeric(value)) #make our values numeric
#change total row to an other row
beerData[nrow(beerData),1] &lt;- &quot;Other&quot;
beerData[nrow(beerData),2] &lt;- sum(beerData[-nrow(beerData),2])
beerPie$params$chartspec &lt;- list(
header = list(
title = list(
text = &quot;Domestic Beer in the U.S. 2013 | Dollar Sales&quot;
),
subtitle = list(
text = &quot;source: The Atlantic, April 2014&quot;
)
)
,data = list(
content = beerData
)
,labels = list(
lines = list(style = &quot;straight&quot;)
)
)
beerPie
</code></pre>
</div>
</div>
</div>
</div>
<script>
function drawchart22d86bc65cf5(){
var params = {
"dom": "chart22d86bc65cf5",
"width": 700,
"height": 400,
"chartspec": {
"header": {
"title": {
"text": "Domestic Beer in the U.S. 2013 | Dollar Sales"
},
"subtitle": {
"text": "source: The Atlantic, April 2014"
}
},
"data": {
"content": {
"label": [ "Bud Light", "Coors Light", "Budweiser", "Miller Light", "Natural Light", "Busch Light", "Michelob Ultra", "Busch", "Keystone Light", "Miller High Life", "Other" ],
"value": [ 5.9468e+09, 2.3604e+09, 2.1104e+09, 1.8689e+09, 1.1228e+09, 8.4428e+08, 7.772e+08, 6.7339e+08, 4.9399e+08, 4.769e+08, 1.6675e+10 ]
}
},
"labels": {
"lines": {
"style": "straight"
}
}
},
"id": "chart22d86bc65cf5"
};
//rCharts defaults to send toJSON as columns
//instead of the expected records for d3pie
//do the transform the hard way for now
function columnsToRecords(data){
var tempdata;
//avoid lodash for now
datakeys = Object.keys(data)
tempdata = data[datakeys[1]].map(function(d,i){
var tempobj = {}
datakeys.forEach(function(key){
tempobj[key] = data[key][i]
})
return tempobj
})
return tempdata;
}
params.chartspec.data.content = columnsToRecords(params.chartspec.data.content);
var pie = new d3pie("#" + params.dom, params.chartspec);
return pie;
};
var chart22d86bc65cf5 = drawchart22d86bc65cf5();
</script>
<script></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