Skip to content

Instantly share code, notes, and snippets.

@patilv
Created October 20, 2013 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patilv/7073094 to your computer and use it in GitHub Desktop.
Save patilv/7073094 to your computer and use it in GitHub Desktop.
Correlation Matrix
library(rCharts)
library(reshape2)
findata=read.csv("https://raw.github.com/patilv/rChartsTutorials/master/findata.csv")
# These are data regarding NCAA athletic department expenses at public universities. Please see the blog post where these charts were originally used
# regarding more details on the origins of these data.: http://analyticsandvisualization.blogspot.com/2013/10/subsidies-revenues-and-expenses-of-ncaa.html
findata=findata[,-c(1:3)] # removing first dummy column - the csv quirk - second column on Rank, and third column on School. Retaining only numeric vars here
corrmatrix<-cor(findata) #store corr matrix
# The following steps are generic and can all be placed in a function with some tweaks to customize output
corrdata=as.data.frame(corrmatrix)
corrdata$Variable1=names(corrdata)
corrdatamelt=melt(corrdata,id="Variable1")
names(corrdatamelt)=c("Variable1","Variable2","CorrelationCoefficient")
corrmatplot = rPlot(Variable2 ~ Variable1, color = 'CorrelationCoefficient', data = corrdatamelt, type = 'tile', height = 600)
corrmatplot$addParams(height = 400, width=800)
corrmatplot$guides("{color: {scale: {type: gradient2, lower: 'red', middle: 'white', upper: 'blue',midpoint: 0}}}")
corrmatplot$guides(y = list(numticks = length(unique(corrdatamelt$Variable1))))
corrmatplot$guides(x = list(numticks = 3))
#corrmatplot$addParams(staggerLabels=TRUE)
corrmatplot
<!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://ramnathv.github.io/rCharts/libraries/widgets/polycharts/js/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 400px;
}
/*
body {
margin-top: 60px;
}
*/
</style>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span8'>
<div class="bs-docs-example">
<div id='chart74cba425f9' class='rChart polycharts'>
</div>
<br/>
<pre><code class='r'>library(rCharts)
library(reshape2)
findata=read.csv(&quot;https://raw.github.com/patilv/rChartsTutorials/master/findata.csv&quot;)
# These are data regarding NCAA athletic department expenses at public universities. Please see the blog post where these charts were originally used
# regarding more details on the origins of these data.: http://analyticsandvisualization.blogspot.com/2013/10/subsidies-revenues-and-expenses-of-ncaa.html
findata=findata[,-c(1:3)] # removing first dummy column - the csv quirk - second column on Rank, and third column on School. Retaining only numeric vars here
corrmatrix&lt;-cor(findata) #store corr matrix
# The following steps are generic and can all be placed in a function with some tweaks to customize output
corrdata=as.data.frame(corrmatrix)
corrdata$Variable1=names(corrdata)
corrdatamelt=melt(corrdata,id=&quot;Variable1&quot;)
names(corrdatamelt)=c(&quot;Variable1&quot;,&quot;Variable2&quot;,&quot;CorrelationCoefficient&quot;)
corrmatplot = rPlot(Variable2 ~ Variable1, color = 'CorrelationCoefficient', data = corrdatamelt, type = 'tile', height = 600)
corrmatplot$addParams(height = 400, width=800)
corrmatplot$guides(&quot;{color: {scale: {type: gradient2, lower: 'red', middle: 'white', upper: 'blue',midpoint: 0}}}&quot;)
corrmatplot$guides(y = list(numticks = length(unique(corrdatamelt$Variable1))))
corrmatplot$guides(x = list(numticks = 3))
#corrmatplot$addParams(staggerLabels=TRUE)
corrmatplot
</code></pre>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
var chartParams = {
"dom": "chart74cba425f9",
"width": 700,
"height": 400,
"layers": [
{
"x": "Variable1",
"y": "Variable2",
"data": {
"Variable1": [ "Total.Revenue", "Total.Expenses", "Total.Subsidy", "Revenue.Less.Expenses", "Total.Revenue", "Total.Expenses", "Total.Subsidy", "Revenue.Less.Expenses", "Total.Revenue", "Total.Expenses", "Total.Subsidy", "Revenue.Less.Expenses", "Total.Revenue", "Total.Expenses", "Total.Subsidy", "Revenue.Less.Expenses" ],
"Variable2": [ "Total.Revenue", "Total.Revenue", "Total.Revenue", "Total.Revenue", "Total.Expenses", "Total.Expenses", "Total.Expenses", "Total.Expenses", "Total.Subsidy", "Total.Subsidy", "Total.Subsidy", "Total.Subsidy", "Revenue.Less.Expenses", "Revenue.Less.Expenses", "Revenue.Less.Expenses", "Revenue.Less.Expenses" ],
"CorrelationCoefficient": [ 1, 0.99054, -0.23493, 0.56712, 0.99054, 1, -0.21984, 0.44872, -0.23493, -0.21984, 1, -0.21049, 0.56712, 0.44872, -0.21049, 1 ]
},
"facet": null,
"color": "CorrelationCoefficient",
"type": "tile",
"height": 600
}
],
"facet": [],
"guides": {
"color": {
"scale": {
"type": "gradient2",
"lower": "red",
"middle": "white",
"upper": "blue",
"midpoint": 0
}
},
"y": {
"numticks": 4
},
"x": {
"numticks": 3
}
},
"coord": [],
"id": "chart74cba425f9"
}
_.each(chartParams.layers, function(el){
el.data = polyjs.data(el.data)
})
var graph_chart74cba425f9 = polyjs.chart(chartParams);
</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