← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-documenters/dhis2/dhis2-docbook-docs] Rev 506: Changed R Google Visulization a bit.

 

------------------------------------------------------------
revno: 506
committer: Jason P. Pickering <jason.p.pickering@xxxxxxxxx>
branch nick: dhis2-docbook-docs
timestamp: Tue 2012-04-24 15:28:29 +0200
message:
  Changed R Google Visulization a bit.
modified:
  src/docbkx/en/dhis2_r.xml
  src/docbkx/en/resources/images/r/google_vis_col_chart.PNG


--
lp:~dhis2-documenters/dhis2/dhis2-docbook-docs
https://code.launchpad.net/~dhis2-documenters/dhis2/dhis2-docbook-docs

Your team DHIS 2 developers is subscribed to branch lp:~dhis2-documenters/dhis2/dhis2-docbook-docs.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-documenters/dhis2/dhis2-docbook-docs/+edit-subscription
=== modified file 'src/docbkx/en/dhis2_r.xml'
--- src/docbkx/en/dhis2_r.xml	2012-04-24 11:53:33 +0000
+++ src/docbkx/en/dhis2_r.xml	2012-04-24 13:28:29 +0000
@@ -336,7 +336,7 @@
   </section>
   <section id="dhis2_r_google_vis">
     <title>Using R, DHIS2 and the Google Visualization API</title>
-    <para>Google&apos;s Visualization API provides a very rich set of tools for the visualization of multi-dimensional data. In this simple example, we will show how to create a simple column chart with the Google Visualization API using the &quot;googleVis&quot; R package. Full information on the package can be found <ulink url="http://code.google.com/p/google-motion-charts-with-r/";>here.</ulink>. The basic principle, as with the other examples, is to get some data from the DHIS2 database, and bring it into R, perform some minor alterations on the data to make it easier to work with, and then create the chart.</para>
+    <para>Google&apos;s Visualization API provides a very rich set of tools for the visualization of multi-dimensional data. In this simple example, we will show how to create a simple motion chart with the Google Visualization API using the &quot;googleVis&quot; R package. Full information on the package can be found <ulink url="http://code.google.com/p/google-motion-charts-with-r/";>here.</ulink>. The basic principle, as with the other examples, is to get some data from the DHIS2 database, and bring it into R, perform some minor alterations on the data to make it easier to work with, and then create the chart. In this case, we will compare ANC1,2,3 data over time and see how they are related with a motion chart.</para>
     <screen>#Load some libraries
 library(RPostgreSQL)
 library(googleVis)
@@ -354,34 +354,35 @@
 host&lt;-&quot;127.0.0.1&quot;
 port&lt;-&quot;5432&quot;
 dbname&lt;-&quot;dhis2_demo&quot;
-con &lt;- dbConnect(PostgreSQL(), user= user, password=password,host=host, port=port,dbname=dbname)
+con &lt;- dbConnect(PostgreSQL(), user= user,
+password=password,host=host, port=port,dbname=dbname)
 #Let&apos;s retrieve some ANC data from the demo database
-sql&lt;-&quot;SELECT ou.shortname as province,i.shortname as indicator,
-extract(year from p.startdate) as year, a.value FROM aggregatedindicatorvalue a
+sql&lt;-&quot;SELECT ou.shortname as province,
+i.shortname as indicator,
+extract(year from p.startdate) as year,
+ a.value
+FROM aggregatedindicatorvalue a
 INNER JOIN  organisationunit ou on a.organisationunitid = ou.organisationunitid
 INNER JOIN indicator i on a.indicatorid = i.indicatorid
 INNER JOIN period p on a.periodid = p.periodid
-where a.indicatorid in (SELECT DISTINCT indicatorid from indicator where shortname ~*(&apos;ANC [123] Coverage&apos;)) and
-a.organisationunitid in (SELECT DISTINCT idlevel2 from _orgunitstructure where idlevel2 is not null)
-and a.periodtypeid = (SELECT DISTINCT periodtypeid from periodtype where name = &apos;Yearly&apos;)&quot;
+WHERE a.indicatorid IN
+(SELECT DISTINCT indicatorid from indicator where shortname ~*(&apos;ANC [123] Coverage&apos;))
+AND a.organisationunitid IN
+ (SELECT DISTINCT idlevel2 from _orgunitstructure where idlevel2 is not null)
+AND a.periodtypeid = (SELECT DISTINCT periodtypeid from periodtype where name = &apos;Yearly&apos;)&quot;
+#Store this in a data frame
 anc&lt;-dfFromSQL(con,sql)
-#Change these columns to factors so that the reshape will work more easily
+#Change these some columns to factors so that the reshape will work more easily
+
 anc$province&lt;-as.factor(anc$province)
 anc$indicator&lt;-as.factor(anc$indicator)
-anc$year&lt;-as.factor(anc$year)
-#Melt and cast the data into the correct format
-anc.m&lt;-melt(anc)
-#Lets only look at 2010 data
-anc.m.2010&lt;-subset(anc.m,year == 2010)
-#Create the cross tab to make it easy 
-#to work with in the chart
-cast(anc.m.2010,province ~ indicator + year)
-colnames(anc.m.2010.ct)&lt;-c(&quot;province&quot;,&quot;ANC1&quot;,&quot;ANC2&quot;,&quot;ANC3&quot;)
-#Create the column chart and plot it
-anc2010.colchart&lt;-gvisColumnChart(anc.m.2010.ct,
-     options=list(title=&quot;ANC Coverage 2010&quot;)
-)
-plot(anc2010.colchart)</screen>
+#We need the time variable as numeric
+anc$year&lt;-as.numeric(as.character(anc$year))
+#Need to cast the table into a slightly different format
+anc&lt;-cast(anc,province + year ~ indicator)
+#Now, create the motion chart and plot it
+M&lt;-gvisMotionChart(anc,idvar=&quot;province&quot;,timevar=&quot;year&quot;)
+plot(M)</screen>
     <para>The resulting graph is displayed below.</para>
     <screenshot>
       <mediaobject>

=== modified file 'src/docbkx/en/resources/images/r/google_vis_col_chart.PNG'
Binary files src/docbkx/en/resources/images/r/google_vis_col_chart.PNG	2012-04-24 07:04:26 +0000 and src/docbkx/en/resources/images/r/google_vis_col_chart.PNG	2012-04-24 13:28:29 +0000 differ