<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>OUseful.Info, the blog... &#187; Rstudio</title>
	<atom:link href="http://blog.ouseful.info/tag/rstudio/feed/?withoutcomments=1" rel="self" type="application/rss+xml" />
	<link>http://blog.ouseful.info</link>
	<description>Trying to find useful things to do with emerging technologies in open education</description>
	<lastBuildDate>Sat, 18 May 2013 11:27:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.ouseful.info' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>OUseful.Info, the blog... &#187; Rstudio</title>
		<link>http://blog.ouseful.info</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.ouseful.info/osd.xml" title="OUseful.Info, the blog..." />
	<atom:link rel='hub' href='http://blog.ouseful.info/?pushpress=hub'/>
		<item>
		<title>More Shiny Goodness &#8211; Tinkering With the Ergast Motor Racing Data API</title>
		<link>http://blog.ouseful.info/2012/12/04/more-shiny-goodness-tinkering-with-the-ergast-motor-racing-data-api/</link>
		<comments>http://blog.ouseful.info/2012/12/04/more-shiny-goodness-tinkering-with-the-ergast-motor-racing-data-api/#comments</comments>
		<pubDate>Tue, 04 Dec 2012 14:14:40 +0000</pubDate>
		<dc:creator>Tony Hirst</dc:creator>
				<category><![CDATA[Rstats]]></category>
		<category><![CDATA[Tinkering]]></category>
		<category><![CDATA[ergastAPI]]></category>
		<category><![CDATA[f1datajunkie]]></category>
		<category><![CDATA[Rstudio]]></category>
		<category><![CDATA[Shiny]]></category>

		<guid isPermaLink="false">http://blog.ouseful.info/?p=9230</guid>
		<description><![CDATA[I had a bit of a play with Shiny over the weekend, using the Ergast Motor Racing Data API and the magical Shiny library for R, that makes building interactive, browser based applications around R a breeze. As this is just a quick heads-up/review post, I&#8217;ll largely limit myself to a few screenshots. When I [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9230&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I had a bit of a play with Shiny over the weekend, using the <a href="http://ergast.com/mrd/">Ergast Motor Racing Data API</a> and the magical <a href="http://www.rstudio.com/shiny/">Shiny library for R</a>, that makes building interactive, browser based applications around R a breeze.</p>
<p>As this is just a quick heads-up/review post, I&#8217;ll largely limit myself to a few screenshots. When I get a chance, I&#8217;ll try to do a bit more of a write-up, though this may actually just take the form of more elaborate documentation of the app, both within the code and in the form of explanatory text in the app itself.</p>
<p>If you want to try ou the app, you can find an instance here: <a href="http://glimmer.rstudio.com/psychemedia/f1ergastdemo">F1 2012 Laptime Explorer</a>. The <a href="https://gist.github.com/4188912">code is also available</a>.</p>
<p>Here&#8217;s the initial view &#8211; the frist race of the season is selected as a default and data loaded in. The driver list is for all drivers represented during the season.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/f1-2012-shiny-ergast-explorer.png?w=700&#038;h=404" alt="f1 2012 shiny ergast explorer" width="700" height="404" class="alignnone size-full wp-image-9254" /></p>
<p>THe driver selectors allow us to just display traces for selected drivers.</p>
<p>The <em>Race History chart</em> is a classic results chart. It show the difference between the race time to date for each driver, by lap, compared to the average lap time for the winner times the lap number. (As such, this is an offline statistic &#8211; it is calculated when the winner&#8217;s overall average laptime is known).</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/race-hisotry-classic-chart.png?w=700&#038;h=350" alt="race hisotry - classic chart" width="700" height="350" class="alignnone size-full wp-image-9253" /></p>
<p>Variants of the classic Race History chart are possible, for example, using different base line times, but I haven&#8217;t implemented any of them  &#8211; or the necessary UI controls. Yet&#8230;</p>
<p>The <em>Lap Chart</em> is another classic:</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/lap-chart-another-classic.png?w=700&#038;h=356" alt="Lap chart - another classic" width="700" height="356" class="alignnone size-full wp-image-9252" /></p>
<p>Annotations for this chart are also supported, describing all drivers who final status was not &#8220;Finished&#8221;.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/lap-chart-with-annotations.png?w=700&#038;h=344" alt="lap chart with annotations" width="700" height="344" class="alignnone size-full wp-image-9251" /></p>
<p>The <em>Lap Evolution chart</em> shows how each driver&#8217;s laptime evolved over the course of the race compared with the fastest overall recorded laptime.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/lap-evolution.png?w=700&#038;h=348" alt="Lap evolution" width="700" height="348" class="alignnone size-full wp-image-9250" /></p>
<p>The <em>Personal Lap Evolution</em> chart shows how each driver&#8217;s laptime evolved over the course of the race compared with their personal fastest laptime.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/personal-lap-evolution.png?w=700&#038;h=349" alt="Personal lap evolution" width="700" height="349" class="alignnone size-full wp-image-9249" /></p>
<p>The <em>Personal Deltas Chart</em> shows the difference between one laptime and the next for each driver.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/personal-deltas.png?w=700&#038;h=366" alt="Personal deltas" width="700" height="366" class="alignnone size-full wp-image-9248" /></p>
<p>The <em>Race Summary Chart</em> is a chart of my own design that tries to capture notable features relating to race position &#8211; the grid position (blue circle), final classification (red circle), position at the end of the first lap (the + or horizontal bar). The violin plot shows the distribution of how many laps the driver spent in each race position. Where the chart is wide, the driver spent a large number of laps in that position.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/race-summary.png?w=700&#038;h=422" alt="race summary" width="700" height="422" class="alignnone size-full wp-image-9247" /></p>
<p>The x-axis ordering pulls out different features about how the race progressed. I need to add in a control that lets the user select different orderings.</p>
<p>Finally, the <em>Fast Lap text scatterplot</em> shows the fastest laptime for each driver and the lap at which they recorded it.</p>
<p><img src="http://ouseful.files.wordpress.com/2012/12/fastlaps.png?w=700&#038;h=357" alt="fastlaps" width="700" height="357" class="alignnone size-full wp-image-9246" /></p>
<p>So &#8211; that&#8217;s a quick review of the app. All in all it took maybe 3 hours getting my head round the data parsing, 2-3 hours figuring what I wanted to do and learning how to do it in Shiny, and a couple of hours doing it/starting to document/annotate it. Next time, it&#8217;ll be much quicker&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/9230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/9230/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9230&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ouseful.info/2012/12/04/more-shiny-goodness-tinkering-with-the-ergast-motor-racing-data-api/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/abbd9f90565ce9ae4d065d93a81d8c03?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">Tony Hirst</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/f1-2012-shiny-ergast-explorer.png" medium="image">
			<media:title type="html">f1 2012 shiny ergast explorer</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/race-hisotry-classic-chart.png" medium="image">
			<media:title type="html">race hisotry - classic chart</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/lap-chart-another-classic.png" medium="image">
			<media:title type="html">Lap chart - another classic</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/lap-chart-with-annotations.png" medium="image">
			<media:title type="html">lap chart with annotations</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/lap-evolution.png" medium="image">
			<media:title type="html">Lap evolution</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/personal-lap-evolution.png" medium="image">
			<media:title type="html">Personal lap evolution</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/personal-deltas.png" medium="image">
			<media:title type="html">Personal deltas</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/race-summary.png" medium="image">
			<media:title type="html">race summary</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/12/fastlaps.png" medium="image">
			<media:title type="html">fastlaps</media:title>
		</media:content>
	</item>
		<item>
		<title>Quick Shiny Demo &#8211; Exploring NHS Winter Sit Rep Data</title>
		<link>http://blog.ouseful.info/2012/11/28/quick-shiny-demo-exploring-nhs-winter-sit-rep-data/</link>
		<comments>http://blog.ouseful.info/2012/11/28/quick-shiny-demo-exploring-nhs-winter-sit-rep-data/#comments</comments>
		<pubDate>Wed, 28 Nov 2012 10:32:19 +0000</pubDate>
		<dc:creator>Tony Hirst</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Infoskills]]></category>
		<category><![CDATA[Rstats]]></category>
		<category><![CDATA[Rstudio]]></category>
		<category><![CDATA[Shiny]]></category>

		<guid isPermaLink="false">http://blog.ouseful.info/?p=9127</guid>
		<description><![CDATA[Having spent a chink of the weekend and a piece of yesterday trying to pull NHS Winter sitrep data into some sort of shape in Scraperwiki, (described, in part, here: When Machine Readable Data Still Causes “Issues” – Wrangling Dates…), I couldn&#8217;t but help myself last night and had a quick go at using RStudio&#8217;s [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9127&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Having spent a chink of the weekend and a piece of yesterday trying to pull NHS Winter sitrep data into some sort of shape in Scraperwiki, (described, in part, here: <a href="http://blog.ouseful.info/2012/11/27/when-machine-readable-data-still-causes-issues-wrangling-dates/">When Machine Readable Data Still Causes “Issues” – Wrangling Dates…</a>), I couldn&#8217;t but help myself last night and had a quick go at using RStudio&#8217;s <a href="http://rstudio.github.com/shiny/tutorial/">Shiny</a> tooling to put together a quick, minimal explorer for it:</p>
<p><a href="http://glimmer.rstudio.com/psychemedia/wightsitrep2/"><img src="http://ouseful.files.wordpress.com/2012/11/wight-sit-reps.png?w=700&#038;h=458" alt="" title="wight sit reps" width="700" height="458" class="alignnone size-full wp-image-9128" /></a></p>
<p>For proof of concept, I just pulled in data relating to the Isle of Wight NHS Trust, but it should be possible to build a more generic explorer: <a href="http://glimmer.rstudio.com/psychemedia/wightsitrep2/">Isle of Wight NHS Sit Rep Explorer Demo</a>.</p>
<p>Three files are used to crate the app &#8211; a script to define the user interface (ui.R), a script to define the server that responds to UI actions and displays the charts (server.R), and a supporting file that creates variables and functions that are globally available to bother the server and UI scripts (global.R).</p>
<pre class="brush: r; title: ; notranslate">##wightsitrep2/global.R

#Loading in CSV directly from https seems to cause problems but this workaround seems okay
floader=function(fn){
  temporaryFile &lt;- tempfile()
  download.file(fn,destfile=temporaryFile, method=&quot;curl&quot;)
  read.csv(temporaryFile)
}

#This is the data source - a scraperwiki API call
#It would make sense to abstract this further, eg allowing the creation of the URL based around a passed in a select statement
u=&quot;https://api.scraperwiki.com/api/1.0/datastore/sqlite?format=csv&amp;name=nhs_sit_reps&amp;query=select%20SHA%2CName%2C%20fromDateStr%2CtoDateStr%2C%20tableName%2CfacetB%2Cvalue%20from%20fulltable%20%20where%20Name%20like%20'%25WIGH%25'&quot;

#Load the data and do a bit typecasting, just in case...
d=floader(u)
d$fdate=as.Date(d$fromDateStr)
d$tdate=as.Date(d$toDateStr)
d$val=as.integer(d$value)</pre>
<pre class="brush: r; title: ; notranslate">##wightsitrep2/ui.R

library(shiny)

tList=levels(d$tableName)
names(tList) = tList

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(
  
  
  # Application title
  headerPanel(&quot;IW NHS Trust Sit Rep Explorer&quot;),
  
  sidebarPanel(
    #Just a single selector here - which table do you want to view?
    selectInput(&quot;tbl&quot;, &quot;Report:&quot;,tList),
    
    div(&quot;This demo provides a crude graphical view over data extracted from&quot;,
        a(href='http://transparency.dh.gov.uk/2012/10/26/winter-pressures-daily-situation-reports-2012-13/',
          &quot;NHS Winter pressures daily situation reports&quot;),
        &quot;relating to the Isle of Wight NHS Trust.&quot;),
    div(&quot;The data is pulled in from a scraped version of the data stored on Scraperwiki&quot;,
        a(href=&quot;https://scraperwiki.com/scrapers/nhs_sit_reps/&quot;,&quot;NHS Sit Reps&quot;),&quot;.&quot;)
    
 ),
  
  #The main panel is where the &quot;results&quot; charts are plotted
  mainPanel(
    plotOutput(&quot;testPlot&quot;),
    tableOutput(&quot;view&quot;)
    
  )
))</pre>
<pre class="brush: r; title: ; notranslate">##wightsitrep2/server.R

library(shiny)
library(ggplot2)

# Define server logic
shinyServer(function(input, output) {
  
  #Do a simple barchart of data in the selected table.
  #Where there are &quot;subtables&quot;, display these using the faceted view
  output$testPlot = reactivePlot(function() {
    g=ggplot(subset(d,fdate&gt;as.Date('2012-11-01') &amp; tableName==input$tbl))
    g=g+geom_bar(aes(x=fdate,y=val),stat='identity')+facet_wrap(~tableName+facetB)
    g=g+theme(axis.text.x=element_text(angle=-90),legend.position=&quot;none&quot;)+labs(title=&quot;Isle of Wight NHS Trust&quot;)
    #g=g+scale_y_discrete(breaks=0:10)
    print(g)
  })
  
  #It would probable make sense to reshape the data presented in this table
  #For example, define columns based on facetB values, so we have one row per date range
  #I also need to sort the table by date
  output$view = reactiveTable(function() {
    head(subset(d,tableName==input$tbl,select=c('Name','fromDateStr','toDateStr','tableName','facetB','value')),n=100)
  })
  
})</pre>
<p>I get the feeling that it shouldn&#8217;t be too hard to create quite complex Shiny apps relatively quickly, pulling on things like Scraperwiki as a remote data source. One thing I haven&#8217;t tried is to use googleVis components, which would support in the first instance at least a sortable table view&#8230; Hmmm&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/9127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/9127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=9127&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ouseful.info/2012/11/28/quick-shiny-demo-exploring-nhs-winter-sit-rep-data/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/abbd9f90565ce9ae4d065d93a81d8c03?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">Tony Hirst</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2012/11/wight-sit-reps.png" medium="image">
			<media:title type="html">wight sit reps</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Simple Interactive Visualisations in R-Studio: Subsetting Data</title>
		<link>http://blog.ouseful.info/2011/08/05/creating-simple-interactive-visualisations-in-r-studio-subsetting-data/</link>
		<comments>http://blog.ouseful.info/2011/08/05/creating-simple-interactive-visualisations-in-r-studio-subsetting-data/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 13:05:00 +0000</pubDate>
		<dc:creator>Tony Hirst</dc:creator>
				<category><![CDATA[Anything you want]]></category>
		<category><![CDATA[dev8d]]></category>
		<category><![CDATA[devsci]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[manipulate]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rstudio]]></category>

		<guid isPermaLink="false">http://blog.ouseful.info/?p=5916</guid>
		<description><![CDATA[Watching a fascinating Google Tech Talk by Hadley Wickham on The Future of Interactive Graphics in R &#8211; A Joint Visualization and UseR Meetup, I was reminded of the manipulate command provided in R-Studio that lets you create slider and dropdown widgets that in turn let you dynamically interact with R based visualisations, for example [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=5916&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Watching a fascinating Google Tech Talk by Hadley Wickham on <a href="http://www.youtube.com/watch?v=iSXNfZESR5I">The Future of Interactive Graphics in R &#8211; A Joint Visualization and UseR Meetup</a>, I was reminded of the <a href="http://support.rstudio.org/help/kb/advanced/interactive-plotting-with-manipulate"><em>manipulate</em> command provided in R-Studio</a> that lets you create slider and dropdown widgets that in turn let you dynamically interact with R based visualisations, for example by setting data ranges or subsetting data.</p>
<p>Here are a couple of quick examples, one using the native plot command, the other using ggplot. In each case, I&#8217;m generating an interactive visualisation that lets me display as a line chart two user selected data series from a larger data set.</p>
<p><a href="http://www.flickr.com/photos/psychemedia/6005652366/" title="Photo Sharing"><img src="http://farm7.static.flickr.com/6141/6005652366_0f788b752c.jpg" width="500" height="327" alt="manipulate UI builder in RStudio" /></a></p>
<p>[<a href="https://gist.github.com/1182997">Data file used in this example</a>]</p>
<p>Here&#8217;s a crude first attempt using <em>plot</em>:</p>
<p><tt>hun_2011comprehensiveLapTimes &lt;- read.csv(&quot;~/code/f1/generatedFiles/hun_2011comprehensiveLapTimes.csv&quot;)<br />
View(hun_2011comprehensiveLapTimes)</p>
<p>library(&quot;manipulate&quot;)<br />
h=un_2011comprehensiveLapTimes</p>
<p>manipulate(<br />
plot(lapTime~lap,data=subset(h,car==cn1),type=&#039;l&#039;,col=car) +<br />
lines(lapTime~lap,data=subset(h,car==cn2 ),col=car),<br />
cn1=slider(1,25),cn2=slider(1,25)<br />
)</tt></p>
<p>This has the form <em>manipulate(<strong>command1+command2</strong>, uiVar=slider(min,max))</em>, so we see for example two R commands to plot the two separate lines, each of them filtered on a value set by the correpsonding slider variable.</p>
<p>Note that we plot the first line using <em>plot</em>, and the second line using <em>lines</em>.</p>
<p>The second approach uses <em>ggplot</em> within the manipulate context:</p>
<p><tt>manipulate(<br />
ggplot(subset(h,h$car==Car_1|car==Car_2)) +<br />
geom_line(aes(y=lapTime,x=lap,group=car,col=car)) +<br />
scale_colour_gradient(breaks=c(Car_1,Car_2),labels=c(Car_1,Car_2)),<br />
Car_1=slider(1,25),Car_2=slider(1,25)<br />
)</tt></p>
<p>In this case, rather than explicitly adding additional line layers, we use the <em>group</em> setting to force the display of lines by group value. The initial <em>ggplot</em> command sets the context, and filters the complete set of timing data down to the timing data associated with at most two cars.</p>
<p>We can add a title to the plot using:</p>
<p><tt>manipulate(<br />
ggplot(subset(h,h$car==Car_1|car==Car_2)) +<br />
geom_line(aes(y=lapTime,x=lap,group=car,col=car)) +<br />
scale_colour_gradient(breaks=c(Car_1,Car_2),labels=c(Car_1,Car_2)) +<br />
opts(title=paste("F1 2011 Hungary: Laptimes for car",Car_1,'and car',Car_2)),<br />
Car_1=slider(1,25),Car_2=slider(1,25)<br />
)</tt></p>
<p>My reading of the <em>manipulate</em> function is that if you make a change to one of the interactive components, the variable values are captured and then passed to the R command sequences, which then executes as normal. (I may be wrong in this assumption of course!) Which is to say: if you write a series of chained R commands, and can abstract out one or more variable values to the start of the sequence, then you can create corresponding interactive UI controls to set those variable values by placing the command series with the <em>manipulate()</em> context.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/5916/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/5916/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=5916&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ouseful.info/2011/08/05/creating-simple-interactive-visualisations-in-r-studio-subsetting-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/abbd9f90565ce9ae4d065d93a81d8c03?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">Tony Hirst</media:title>
		</media:content>

		<media:content url="http://farm7.static.flickr.com/6141/6005652366_0f788b752c.jpg" medium="image">
			<media:title type="html">manipulate UI builder in RStudio</media:title>
		</media:content>
	</item>
		<item>
		<title>First Play With R and R-Studio &#8211; F1 Lap Time Box Plots</title>
		<link>http://blog.ouseful.info/2011/05/05/first-play-with-r-and-r-studio-f1-lap-time-box-plots/</link>
		<comments>http://blog.ouseful.info/2011/05/05/first-play-with-r-and-r-studio-f1-lap-time-box-plots/#comments</comments>
		<pubDate>Thu, 05 May 2011 13:56:03 +0000</pubDate>
		<dc:creator>Tony Hirst</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Infoskills]]></category>
		<category><![CDATA[onlinejournalismblog]]></category>
		<category><![CDATA[Visualisation]]></category>
		<category><![CDATA[ddj]]></category>
		<category><![CDATA[f1data]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R-studio]]></category>
		<category><![CDATA[Rstudio]]></category>

		<guid isPermaLink="false">http://blog.ouseful.info/?p=5416</guid>
		<description><![CDATA[Last summer, at the European Centre for Journalism round table on data driven journalism, I remember saying something along the lines of &#8220;your eyes can often do the stats for you&#8221;, the implication being that our perceptual apparatus is good at pattern detection, and can often see things in the data that most of us [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=5416&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Last summer, at the European Centre for Journalism round table on data driven journalism, I remember saying something along the lines of &#8220;your eyes can often do the stats for you&#8221;, the implication being that our perceptual apparatus is good at pattern detection, and can often see things in the data that most of us would miss using the very limited range of statistical tools that we are either aware of, or are comfortable using.</p>
<p>I don&#8217;t know how good a statistician you need to be to distinguish between Anscombe&#8217;s quartet, but the differences are obvious to the eye:</p>
<div class="wp-caption alignnone" style="width: 690px"><a href="http://en.wikipedia.org/wiki/Anscombe's_quartet"><img alt="" src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Anscombe%27s_quartet_3.svg/800px-Anscombe%27s_quartet_3.svg.pngA" title="Anscombe&#039;s quartet - wikipedia" width="670" height="500" /></a><p class="wp-caption-text">Anscombe&#039;s quartet /via Wikipedia</p></div>
<p>Another shamistician (h/t @daveyp) heuristic (or maybe it&#8217;s a crapistician rule of thumb?!) might go something along the lines of: &#8220;if you use the right visualisations, you don&#8217;t necessarily need to do any statistics yourself&#8221;. In this case, the implication is that if you choose a viualisation technique that embodies or implements a statistical process in some way, the maths is done for you, and you get to see what the statistical tool has uncovered.</p>
<p>Now I know that as someone working in education, I&#8217;m probably supposed to uphold the &#8220;should learn it properly&#8221; principle&#8230; But needing to know statistics in order to benefit from the use of statistical tools seems to me to be a massive barrier to entry in the use of this technology (statistics is a technology&#8230;) You just need to know how to use the technology appropriately, or at least, not use it &#8220;dangerously&#8221;&#8230;</p>
<p>So to this end (&#8220;democratising access to technology&#8221;), I thought it was about time I started to play with R, the statistical programming language (and rival to SPSS?) that appears to have a certain amount of traction at the moment given the number of books about to come out around it&#8230; R is a command line language, but the recently released <a href="http://www.rstudio.org/">R-Studio</a> seems to offer an easier way in, so I thought I&#8217;d go with that&#8230;</p>
<p>Flicking through <a href="http://www.amazon.co.uk/First-Course-Statistical-Programming/dp/0521694248?tag=ouseful-21">A First Course in Statistical Programming with R</a>, a book I bought a few weeks ago in the hope that the osmotic reading effect would give me some idea as to what it&#8217;s possible to do with R, I found a command line example showing how to create a simple box plot (box and whiskers plot) that I could understand enough to feel confident I could change&#8230;</p>
<p>Having an F1 data set/CSV file to hand (laptimes and <a href="http://www.raeng.org.uk/education/diploma/maths/pdf/exemplars_advanced/14_Car_Racing.pdf">fuel adjusted laptimes</a>) from the China 2001 grand prix, I thought I&#8217;d see how easy it was to just dive in&#8230; And it was 2 minutes easy&#8230; (If you want to play along, <a href="https://gist.github.com/957008">here&#8217;s the data file</a>).</p>
<p><img src="http://ouseful.files.wordpress.com/2011/05/rstudio-f1-laptimes-demo.jpg?w=700&#038;h=540" alt="" title="rstudio - f1 laptimes demo" width="700" height="540" class="alignnone size-full wp-image-5425" /></p>
<p>Here&#8217;s the command I used:<br />
<tt>boxplot(Lap.Time ~ Driver, data=lapTimeFuel)</tt></p>
<p>Remembering a comment in a Making up the Numbers blogpost (<a href="http://f1numbers.wordpress.com/2010/03/16/driver-consistency-bahrain-2010/">Driver Consistency – Bahrain 2010</a>) about the effect on laptime distributions from removing opening, in and out lap times, a  quick Google turned up a way of quickly stripping out slow times. (This isn&#8217;t as clean as removing the actual opening, in and out lap times &#8211; it also removes mistake laps, for example, but I&#8217;m just exploring, right? Right?!;-)</p>
<p><tt>lapTime2 &lt;- subset(lapTimeFuel, Lap.Time &lt; 110.1)</tt></p>
<p>I could then plot the distribution in the reduced <em>lapTime2</em> dataset by changing the original boxplot command to use (<tt>data=lapTime2</tt>). (Note that as with many interactive editors, using your keyboard&#8217;s up arrow displays previously entered commands in the current command line; so you can re-enter a previously entered command by hitting the up arrow a few times, then entering return. You can also edit the current command line, using the left and right arrow keys to move the cursor, and the delete key to delete text.)</p>
<p>Prior programming experience suggests this should also work&#8230;</p>
<p><tt>boxplot(Lap.Time ~ Driver, data=subset(lapTimeFuel, Lap.Time &lt; 110))</tt></p>
<p>Something else I tried was to look at the distribution of fuel weight adjusted laptimes (where the time penalty from the weight of the fuel in the car is removed):</p>
<p><tt>boxplot(Fuel.Adjusted.Laptime ~ Driver, data=lapTimeFuel)</tt></p>
<p>Looking at the <a href="http://www.rstudio.org/docs/release_notes_v0.93">release notes</a> for the latest version of R-Studio suggests that you can build interactive controls into your plots (a bit like Mathematica supports?). The example provided shows how to change the x-range on a plot:<br />
<tt>manipulate(<br />
  plot(cars, xlim=c(0,x.max)),<br />
  x.max=slider(15,25))</tt></p>
<p>Hmm&#8230; can we set the filter value dynamically I wonder?</p>
<p><tt>manipulate(<br />
boxplot(Lap.Time ~ Driver, data=subset(lapTimeFuel, Lap.Time &lt; maxval)),<br />
 maxval=slider(100,140))</tt></p>
<p><img src="http://ouseful.files.wordpress.com/2011/05/r-studio-manipulate-interactive-component.png?w=700&#038;h=396" alt="" title="R-studio manipulate interactive component" width="700" height="396" class="alignnone size-full wp-image-5426" /></p>
<p>Seems like it&#8230;?:-) We can also combine interactive controls:</p>
<p><tt>manipulate(boxplot(Lap.Time ~ Driver, data=subset(lapTimeFuel, Lap.Time &lt; maxval),outline=outline),maxval=slider(100,140),outline = checkbox(FALSE, &quot;Show outliers&quot;))</tt></p>
<p><img src="http://ouseful.files.wordpress.com/2011/05/r-studio-combining-interactive-controls.png?w=700&#038;h=372" alt="" title="R-Studio - combining interactive controls" width="700" height="372" class="alignnone size-full wp-image-5427" /></p>
<p>Okay &#8211; that&#8217;s enough for now&#8230; I reckon that with a handful of commands on a crib sheet, you can probably get quite a lot of chart plot visualisations done, as well as statistical visualisations, in the R-Studio environment; it also seems easy enough to build in interactive controls that let you play with the data in a visually interactive way&#8230;</p>
<p>The trick comes from choosing visual statistics approaches to analyse your data that don&#8217;t break any of the assumptions about the data that the particular statistical approach relies on in order for it to be applied in any sensible or meaningful way.</p>
<p>[This blog post is written, in part, as a way for me to try to come up with something to say at the OU Statistics Group's one day conference on <a href="http://mcs.open.ac.uk/su379/VIPS/">Visualisation and Presentation in Statistics</a>. One idea I wanted to explore was: visualisations are powerful; visualisation techniques may incorporate statistical methods or let you "see" statistical patterns; most people know very little statistics; that shouldnlt stop them being able to use statistics as a technology; so what are we going to do about it? Feedback welcome... Err....?!]</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/5416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/5416/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ouseful.info&#038;blog=325417&#038;post=5416&#038;subd=ouseful&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ouseful.info/2011/05/05/first-play-with-r-and-r-studio-f1-lap-time-box-plots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/abbd9f90565ce9ae4d065d93a81d8c03?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">Tony Hirst</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Anscombe%27s_quartet_3.svg/800px-Anscombe%27s_quartet_3.svg.pngA" medium="image">
			<media:title type="html">Anscombe&#039;s quartet - wikipedia</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2011/05/rstudio-f1-laptimes-demo.jpg" medium="image">
			<media:title type="html">rstudio - f1 laptimes demo</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2011/05/r-studio-manipulate-interactive-component.png" medium="image">
			<media:title type="html">R-studio manipulate interactive component</media:title>
		</media:content>

		<media:content url="http://ouseful.files.wordpress.com/2011/05/r-studio-combining-interactive-controls.png" medium="image">
			<media:title type="html">R-Studio - combining interactive controls</media:title>
		</media:content>
	</item>
	</channel>
</rss>
