block by ramnathv 7576061

Regression Line with rCharts

Full Screen

index.html

<!doctype HTML>
<meta charset = 'utf-8'>
<html>
  <head>
    <link rel='stylesheet' href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
    <link rel='stylesheet' href="//getbootstrap.com/2.3.2/assets/js/google-code-prettify/prettify.css">
    <link rel='stylesheet' href="//aozora.github.io/bootplus/assets/css/docs.css">
    
    <script src='//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='chartfeac499e69a4' class='rChart polycharts'>
            </div> 
          <br/>
<pre><code class='r'># Regression Plots using rCharts
require(ggplot2)
require(rCharts)

dat = fortify(lm(mpg ~ wt, data = mtcars))
names(dat) = gsub('\\.', '_', names(dat))

p1 &lt;- rPlot(mpg ~ wt, data = dat, type = 'point')
p1$layer(y = '_fitted', copy_layer = T, type = 'line',
  color = list(const = 'red'),
  tooltip = &quot;function(item){return item._fitted}&quot;)
p1
</code></pre>
</div>
        </div>
      </div>
    </div>
      
    <script type='text/javascript'>
    var chartParams = {
 "dom": "chartfeac499e69a4",
"width":    700,
"height":    400,
"layers": [
 {
 "x": "wt",
"y": "mpg",
"data": {
 "mpg": [     21,     21,   22.8,   21.4,   18.7,   18.1,   14.3,   24.4,   22.8,   19.2,   17.8,   16.4,   17.3,   15.2,   10.4,   10.4,   14.7,   32.4,   30.4,   33.9,   21.5,   15.5,   15.2,   13.3,   19.2,   27.3,     26,   30.4,   15.8,   19.7,     15,   21.4 ],
"wt": [   2.62,  2.875,   2.32,  3.215,   3.44,   3.46,   3.57,   3.19,   3.15,   3.44,   3.44,   4.07,   3.73,   3.78,   5.25,  5.424,  5.345,    2.2,  1.615,  1.835,  2.465,   3.52,  3.435,   3.84,  3.845,  1.935,   2.14,  1.513,   3.17,   2.77,   3.57,   2.78 ],
"_hat": [ 0.043269, 0.035197, 0.058376, 0.03125, 0.032922, 0.033236, 0.035443, 0.031275, 0.031402, 0.032922, 0.032922, 0.055752, 0.040109, 0.041921, 0.17048, 0.19533, 0.18379, 0.066117, 0.11775, 0.095627, 0.050317, 0.034338, 0.032848, 0.044317, 0.044528, 0.086649, 0.070351, 0.12911, 0.031325, 0.03799, 0.035443, 0.037692 ],
"_sigma": [ 3.0675, 3.0931, 3.0721, 3.0883, 3.0977, 3.0952, 3.0087, 2.9967, 3.0661, 3.0974,  3.091, 3.0935, 3.0979, 3.0773, 3.0887, 3.0672, 2.8436, 2.8024, 3.0787, 2.8328, 3.0577, 3.0466,  3.017, 3.0273, 3.0624, 3.0972, 3.0978, 3.0887,  2.977, 3.0529, 3.0381, 3.0918 ],
"_cooksd": [ 0.013274, 0.001724, 0.015439, 0.0030206, 7.5996e-05, 0.00092106, 0.031314, 0.031139, 0.0099619, 0.00017058, 0.0022962, 0.0025325, 5.9233e-06, 0.0087273, 0.018383, 0.071925, 0.53191, 0.19299, 0.02486, 0.25987, 0.020498, 0.017536, 0.026287, 0.03135, 0.015964, 0.00071116, 0.00010142, 0.013235, 0.037136, 0.017109, 0.021094, 0.0023159 ],
"_fitted": [ 23.283,  21.92, 24.886, 20.103,   18.9, 18.793, 18.205, 20.236,  20.45,   18.9,   18.9, 15.533,  17.35, 17.083, 9.2267, 8.2967, 8.7189, 25.527, 28.654, 27.478, 24.111, 18.473, 18.927, 16.762, 16.736, 26.944, 25.848, 29.199, 20.343, 22.481, 18.205, 22.427 ],
"_resid": [ -2.2826, -0.91977, -2.086, 1.2973, -0.20014, -0.69325, -3.9054, 4.1637,   2.35, 0.29986, -1.1001, 0.86687, -0.050247, -1.883, 1.1733, 2.1033, 5.9811, 6.8727, 1.7462,  6.422, -2.611, -2.9726, -3.7269, -3.4624, 2.4644, 0.35643, 0.15204, 1.2011, -4.5432, -2.7809, -3.2054, -1.0275 ],
"_stdresid": [ -0.76617, -0.30743, -0.70575, 0.43275, -0.066819, -0.23148, -1.3055, 1.3889, 0.78393, 0.10011, -0.36729, 0.29289, -0.016838, -0.6316, 0.42296, 0.7698, 2.1735, 2.3349, 0.61036, 2.2171, -0.87964, -0.99314, -1.2442, -1.1628, 0.82772, 0.12244, 0.051772, 0.42254, -1.5155, -0.93087, -1.0715, -0.34388 ] 
},
"facet": null,
"type": "point" 
},
{
 "y": "_fitted",
"type": "line",
"color": {
 "const": "red" 
},
"tooltip": "function(item){return item._fitted}",
"x": "wt",
"data": {
 "mpg": [     21,     21,   22.8,   21.4,   18.7,   18.1,   14.3,   24.4,   22.8,   19.2,   17.8,   16.4,   17.3,   15.2,   10.4,   10.4,   14.7,   32.4,   30.4,   33.9,   21.5,   15.5,   15.2,   13.3,   19.2,   27.3,     26,   30.4,   15.8,   19.7,     15,   21.4 ],
"wt": [   2.62,  2.875,   2.32,  3.215,   3.44,   3.46,   3.57,   3.19,   3.15,   3.44,   3.44,   4.07,   3.73,   3.78,   5.25,  5.424,  5.345,    2.2,  1.615,  1.835,  2.465,   3.52,  3.435,   3.84,  3.845,  1.935,   2.14,  1.513,   3.17,   2.77,   3.57,   2.78 ],
"_hat": [ 0.043269, 0.035197, 0.058376, 0.03125, 0.032922, 0.033236, 0.035443, 0.031275, 0.031402, 0.032922, 0.032922, 0.055752, 0.040109, 0.041921, 0.17048, 0.19533, 0.18379, 0.066117, 0.11775, 0.095627, 0.050317, 0.034338, 0.032848, 0.044317, 0.044528, 0.086649, 0.070351, 0.12911, 0.031325, 0.03799, 0.035443, 0.037692 ],
"_sigma": [ 3.0675, 3.0931, 3.0721, 3.0883, 3.0977, 3.0952, 3.0087, 2.9967, 3.0661, 3.0974,  3.091, 3.0935, 3.0979, 3.0773, 3.0887, 3.0672, 2.8436, 2.8024, 3.0787, 2.8328, 3.0577, 3.0466,  3.017, 3.0273, 3.0624, 3.0972, 3.0978, 3.0887,  2.977, 3.0529, 3.0381, 3.0918 ],
"_cooksd": [ 0.013274, 0.001724, 0.015439, 0.0030206, 7.5996e-05, 0.00092106, 0.031314, 0.031139, 0.0099619, 0.00017058, 0.0022962, 0.0025325, 5.9233e-06, 0.0087273, 0.018383, 0.071925, 0.53191, 0.19299, 0.02486, 0.25987, 0.020498, 0.017536, 0.026287, 0.03135, 0.015964, 0.00071116, 0.00010142, 0.013235, 0.037136, 0.017109, 0.021094, 0.0023159 ],
"_fitted": [ 23.283,  21.92, 24.886, 20.103,   18.9, 18.793, 18.205, 20.236,  20.45,   18.9,   18.9, 15.533,  17.35, 17.083, 9.2267, 8.2967, 8.7189, 25.527, 28.654, 27.478, 24.111, 18.473, 18.927, 16.762, 16.736, 26.944, 25.848, 29.199, 20.343, 22.481, 18.205, 22.427 ],
"_resid": [ -2.2826, -0.91977, -2.086, 1.2973, -0.20014, -0.69325, -3.9054, 4.1637,   2.35, 0.29986, -1.1001, 0.86687, -0.050247, -1.883, 1.1733, 2.1033, 5.9811, 6.8727, 1.7462,  6.422, -2.611, -2.9726, -3.7269, -3.4624, 2.4644, 0.35643, 0.15204, 1.2011, -4.5432, -2.7809, -3.2054, -1.0275 ],
"_stdresid": [ -0.76617, -0.30743, -0.70575, 0.43275, -0.066819, -0.23148, -1.3055, 1.3889, 0.78393, 0.10011, -0.36729, 0.29289, -0.016838, -0.6316, 0.42296, 0.7698, 2.1735, 2.3349, 0.61036, 2.2171, -0.87964, -0.99314, -1.2442, -1.1628, 0.82772, 0.12244, 0.051772, 0.42254, -1.5155, -0.93087, -1.0715, -0.34388 ] 
},
"facet": null 
} 
],
"facet": [],
"guides": [],
"coord": [],
"id": "chartfeac499e69a4" 
}
    _.each(chartParams.layers, function(el){
        el.data = polyjs.data(el.data)
    })
    var graph_chartfeac499e69a4 = polyjs.chart(chartParams);
</script>
    
  </body>
  <!-- Google Prettify -->
  <script src="//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>

code.R

# Regression Plots using rCharts
require(ggplot2)
require(rCharts)

dat = fortify(lm(mpg ~ wt, data = mtcars))
names(dat) = gsub('\\.', '_', names(dat))

p1 <- rPlot(mpg ~ wt, data = dat, type = 'point')
p1$layer(y = '_fitted', copy_layer = T, type = 'line',
  color = list(const = 'red'),
  tooltip = "function(item){return item._fitted}")
p1