{"id":1740,"date":"2014-11-22T12:16:54","date_gmt":"2014-11-22T12:16:54","guid":{"rendered":"http:\/\/brain.payzip.co.kr\/?p=1740"},"modified":"2014-11-22T12:16:54","modified_gmt":"2014-11-22T12:16:54","slug":"r-interactive-visualization-from-r-to-d3-using-rcharts","status":"publish","type":"post","link":"https:\/\/blog.goorigo.com\/?p=1740","title":{"rendered":"R : Interactive visualization: from R to D3 using rCharts"},"content":{"rendered":"<article id=\"post-137\" class=\"post-137 post type-post status-publish format-standard hentry category-d3 category-data-visualization category-r category-spatial\">\n<header class=\"entry-header\">\n<h1 class=\"entry-title\"><a title=\"http:\/\/zevross.com\/blog\/2014\/04\/03\/interactive-visualization-from-r-to-d3-using-rcharts\/\" href=\"http:\/\/zevross.com\/blog\/2014\/04\/03\/interactive-visualization-from-r-to-d3-using-rcharts\/\" target=\"_blank\">http:\/\/zevross.com\/blog\/2014\/04\/03\/interactive-visualization-from-r-to-d3-using-rcharts\/<\/a><\/h1>\n<h1 class=\"entry-title\">Interactive visualization: from R to D3 using <code>rCharts<\/code><\/h1>\n<div class=\"entry-meta\"><span class=\"posted-on\">Posted on <a href=\"http:\/\/zevross.com\/blog\/2014\/04\/03\/interactive-visualization-from-r-to-d3-using-rcharts\/\" rel=\"bookmark\"><time class=\"entry-date published\" datetime=\"2014-04-03T17:48:17+00:00\">April 3, 2014<\/time><\/a> <\/span><span class=\"byline\">by <span class=\"author vcard\"><a class=\"url fn n\" href=\"http:\/\/zevross.com\/blog\/author\/zevzevross-com\/\">zev@zevross.com<\/a><\/span><\/span><\/div>\n<\/header>\n<div class=\"entry-content\">\n<p>Data Driven Documents, or D3 for short, is an incredible JavaScript library for creating interactive data visualization on the web. Earlier this year, for example, we illustrated the power of D3 by interactively linking maps and charts in <a href=\"http:\/\/www.zevross.com\/tech\/d3\/\">this visualization<\/a>.<\/p>\n<p>D3, however, can be challenging to work with, especially if you don&#8217;t have experience with JavaScript. Luckily, for those who work with R, the package <code>rCharts<\/code>, created by Ramnath Vaidyanathan, makes moving your R visualizations to the web much easier. The package is designed to help users to create interactive JavaScript visualizations directly from R. One of the nice features of <code>rCharts<\/code> is that it does not limit itself to a single JavaScript library but rather provides the capacity to create charts using a wide range of libraries including Polychart, Morris, NVD3, xCharts, HighCharts, Rickshaw and even maps using Leaflet. In this example, though, we will focus on D3.<\/p>\n<h2>An example using <code>rCharts<\/code>: spatial packages in R through time<\/h2>\n<p>In order to test out the package we&#8217;re going to develop a line plot showing the growth in the number of spatial data-related packages in R. Spatial data-related packages will be defined as those listed on the <a href=\"http:\/\/cran.r-project.org\/web\/views\/Spatial.html\">Analysis of Spatial Data Task View<\/a> on the Comprehensive R Archive Network (CRAN) and maintained by Roger Bivand.<\/p>\n<h3>1. Install <code>rCharts<\/code> from GitHub<\/h3>\n<p>If you haven&#8217;t already installed rCharts, unfortunately, it&#8217;s not quite as simple as typing<code>install.packages(\"rCharts\")<\/code>. Instead you will need to load it from GitHub using a helper function from the devtools library created by Hadley Wickham. To install you can use the following code:<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-keyword\">library<\/span>(devtools)\ninstall_github(<span class=\"hljs-string\">'rCharts'<\/span>, <span class=\"hljs-string\">'ramnathv'<\/span>)\n\n<\/code><\/pre>\n<h3>2. Use the <code>XML<\/code> package to grab the list of spatial packages<\/h3>\n<p>Now you&#8217;re ready to begin coding. We will load the XML package to help us grab the list of spatial packages from CRAN and then we do a little formatting.<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-keyword\">require<\/span>(XML)\n<span class=\"hljs-keyword\">require<\/span>(rCharts)\n\n<span class=\"hljs-comment\"># URL of the task view (thanks Roger Bivand)<\/span>\nurl&lt;-<span class=\"hljs-string\">\"http:\/\/cran.r-project.org\/web\/views\/Spatial.html\"<\/span>\n\n<span class=\"hljs-comment\"># grab list of packages<\/span>\nspat&lt;-readHTMLList(url, stringsAsFactors = <span class=\"hljs-literal\">F<\/span>)[[<span class=\"hljs-number\">2<\/span>]]\n\n<span class=\"hljs-comment\"># For cleanliness drop the word '(core)' from the package name<\/span>\nspat&lt;-gsub(<span class=\"hljs-string\">\" \\\\(core\\\\)\"<\/span>, <span class=\"hljs-string\">\"\"<\/span>, spat)\n<\/code><\/pre>\n<h3>3. Create a function to get package dates<\/h3>\n<p>For each package I want to know the most recent date it was published as well as the very first publish date. To do this we will cycle through the package names and for each we will get the most recent date from the main package site and then we will get the earliest package date from the package&#8217;s archive page. If a package does not have an archive (meaning it&#8217;s a new package) then we will use the current date as the first date. Here is the function:<\/p>\n<pre><code class=\"r hljs \">\ngetPkgInfo&lt;-<span class=\"hljs-keyword\">function<\/span>(pkg){\n  url = paste(<span class=\"hljs-string\">\"http:\/\/cran.r-project.org\/web\/packages\/\"<\/span>, pkg, <span class=\"hljs-string\">\"\/index.html\"<\/span>, sep=<span class=\"hljs-string\">\"\"<\/span>)\n\n  <span class=\"hljs-comment\"># get table of details and extract most current date<\/span>\n  dtl&lt;-readHTMLTable(url, stringsAsFactors = <span class=\"hljs-literal\">F<\/span>)[[<span class=\"hljs-number\">1<\/span>]]\n  curdate&lt;-dtl[which(dtl[,<span class=\"hljs-number\">1<\/span>]==<span class=\"hljs-string\">\"Published:\"<\/span>),<span class=\"hljs-number\">2<\/span>]\n\n  <span class=\"hljs-comment\"># get first archive date<\/span>\n  url = paste(<span class=\"hljs-string\">'http:\/\/cran.r-project.org\/src\/contrib\/Archive\/'<\/span>, pkg, <span class=\"hljs-string\">'\/'<\/span>, sep=<span class=\"hljs-string\">\"\"<\/span>)\n  packages = <span class=\"hljs-keyword\">try<\/span>(readHTMLTable(url, stringsAsFactors = <span class=\"hljs-literal\">F<\/span>)[[<span class=\"hljs-number\">1<\/span>]][-<span class=\"hljs-number\">1<\/span>,])\n\n  <span class=\"hljs-comment\"># if there is no archive first date is current date<\/span>\n  <span class=\"hljs-keyword\">if<\/span>(class(packages)==<span class=\"hljs-string\">'try-error'<\/span>){\n    print(<span class=\"hljs-string\">\"in error\"<\/span>)\n    firstdate&lt;-as.Date(curdate)\n\n  <span class=\"hljs-comment\">#otherwise get first archive date<\/span>\n  }<span class=\"hljs-keyword\">else<\/span>{ \n\n    packages&lt;-packages[-<span class=\"hljs-number\">1<\/span>,]\n    firstdate&lt;-as.Date(packages[<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-string\">\"Last modified\"<\/span>],<span class=\"hljs-string\">\"%d-%b-%Y %H:%M\"<\/span>)\n\n\n  }\n  <span class=\"hljs-keyword\">return<\/span>(data.frame(pkg=pkg, firstdate=firstdate, curdate=curdate))\n}\n<\/code><\/pre>\n<h3>4. Use the function to assemble the table<\/h3>\n<p>We will run the function on all package names using <code>lapply<\/code> and then bind them together into one table using <code>do.call<\/code>.<\/p>\n<pre><code class=\"r hljs \">\npkgdates&lt;-do.call(<span class=\"hljs-string\">\"rbind\"<\/span>, lapply(spat, getPkgInfo))\n\npkgdates&lt;-pkgdates[order(pkgdates$firstdate),]\npkgdates$numpkg&lt;-<span class=\"hljs-number\">1<\/span>:nrow(pkgdates)\npkgdates$datechar&lt;-as.character(pkgdates$firstdate)\nhead(pkgdates)\n<span class=\"hljs-comment\">##          pkg  firstdate    curdate numpkg   datechar<\/span>\n<span class=\"hljs-comment\">## 8      akima 1998-08-20 2013-09-16      1 1998-08-20<\/span>\n<span class=\"hljs-comment\">## 124  tripack 1998-08-20 2013-09-16      2 1998-08-20<\/span>\n<span class=\"hljs-comment\">## 10       ash 1999-04-21 2013-02-11      3 1999-04-21<\/span>\n<span class=\"hljs-comment\">## 92  sgeostat 1999-04-21 2013-03-01      4 1999-04-21<\/span>\n<span class=\"hljs-comment\">## 65      nlme 1999-11-23 2014-03-31      5 1999-11-23<\/span>\n<span class=\"hljs-comment\">## 115  splancs 2000-11-05 2013-09-01      6 2000-11-05<\/span>\n<\/code><\/pre>\n<h3>5. Create the chart using <code>rCharts<\/code><\/h3>\n<p>Now that we have the table we want to use, creating a D3 chart using <code>rCharts<\/code> is straightforward. Note that <code>rCharts<\/code> uses NVD3.js, a library designed to build re-usable charts for D3, to create these plots. The function for creating a D3 chart is nPlot and you use it like this:<\/p>\n<pre><code class=\"r hljs \">\nd3chart&lt;-nPlot(numpkg~firstdate, data=pkgdates, type=<span class=\"hljs-string\">\"lineChart\"<\/span>)\n<\/code><\/pre>\n<h3>6. Customize your chart<\/h3>\n<p>I found that the default tooltip and x-axis format for dates were not what I wanted so I used the following code to improve on the defaults. I did find the rCharts documentation to be lacking in places. Finding the right approach to altering the tooltips and the axes took some digging. Hopefully the documentation will be improved in the near future. In the meantime, the <a href=\"http:\/\/rcharts.readthedocs.org\/en\/latest\/index.html\">rCharts readthedocs<\/a> is a good place to start. Here is the code to modify the tooltip and x-axis:<\/p>\n<pre><code class=\"r hljs \">\nd3chart$chart(tooltipContent = <span class=\"hljs-string\">\"#! function(key, x, y, e){ \n  return 'Package: ' + e.point.pkg\n} !#\"<\/span>)\n\n\nd3chart$xAxis(\n  tickFormat = <span class=\"hljs-string\">\"#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#\"<\/span>\n)\n<\/code><\/pre>\n<h3>6. Finally, you&#8217;re ready to save and publish your result<\/h3>\n<p><code>rCharts<\/code> has a number of ways to save and publish your charts. To simply save to the current working directory you can use the following code:<\/p>\n<pre><code class=\"r hljs \">d3chart$save(<span class=\"hljs-string\">'d3chart.html'<\/span>, cdn=<span class=\"hljs-literal\">TRUE<\/span>)\n<\/code><\/pre>\n<p>This saves an HTML file in your working directory. Note that, by default, the paths to dependencies will be on your computer. If you want the script to point to a hosted source of these dependencies (a content delivery network\/CDN) you will need to include the argument <code>cdn=TRUE<\/code>.<\/p>\n<p>A very nice additional piece of functionality is the ability to publish your chart as a gist at GitHub. You simply run the following code \u2013 it will ask you for your username and password and add the gist to GitHub. You can view your gist at<a href=\"http:\/\/rcharts.github.io\/viewer\/XXXXX\">http:\/\/rcharts.github.io\/viewer\/XXXXX<\/a> where XXXXX is the gist id.<\/p>\n<pre><code class=\"r hljs \">d3chart$publish(<span class=\"hljs-string\">'rCharts D3 Spatial Packages'<\/span>, host = <span class=\"hljs-string\">'gist'<\/span>)\n<\/code><\/pre>\n<h3>Here is the final result, hover over the line to identify the packages published on that date<\/h3>\n<p><iframe loading=\"lazy\" src=\"http:\/\/bl.ocks.org\/zross\/raw\/9959347\/\" width=\"800\" height=\"600\"><\/iframe><\/p>\n<\/div>\n<footer class=\"entry-meta entry-footer\"><span class=\"cat-links\">Posted in <a title=\"View all posts in D3\" href=\"http:\/\/zevross.com\/blog\/category\/d3\/\" rel=\"category tag\">D3<\/a>, <a title=\"View all posts in Data Visualization\" href=\"http:\/\/zevross.com\/blog\/category\/data-visualization\/\" rel=\"category tag\">Data Visualization<\/a>, <a title=\"View all posts in R\" href=\"http:\/\/zevross.com\/blog\/category\/r\/\" rel=\"category tag\">R<\/a>, <a title=\"View all posts in Spatial\" href=\"http:\/\/zevross.com\/blog\/category\/spatial\/\" rel=\"category tag\">Spatial<\/a><\/span><\/footer>\n<\/article>\n<nav class=\"navigation post-navigation\">\n<div class=\"nav-links\"><\/div>\n<\/nav>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/zevross.com\/blog\/2014\/04\/03\/interactive-visualization-from-r-to-d3-using-rcharts\/ Interactive visualization: from R to D3 using rCharts Posted on April 3, 2014 by zev@zevross.com Data Driven Documents, or D3 for short, is an incredible JavaScript library for creating&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/1740"}],"collection":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1740"}],"version-history":[{"count":0,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/1740\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}