<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Open Source | Robin Lovelace</title><link>https://robinlovelace.net/old-site/tag/open-source/</link><atom:link href="https://robinlovelace.net/old-site/tag/open-source/index.xml" rel="self" type="application/rss+xml"/><description>Open Source</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Wed, 03 Sep 2025 14:00:00 +0100</lastBuildDate><image><url>https://robinlovelace.net/old-site/media/icon_hu93dbabadc2a9bdd4930d1377c0b338b2_5137_512x512_fill_lanczos_center_3.png</url><title>Open Source</title><link>https://robinlovelace.net/old-site/tag/open-source/</link></image><item><title>Future-Proof Transport Planning: Cities Workshop</title><link>https://robinlovelace.net/old-site/talk/future-proof-transport-planning-cities-workshop/</link><pubDate>Wed, 03 Sep 2025 14:00:00 +0100</pubDate><guid>https://robinlovelace.net/old-site/talk/future-proof-transport-planning-cities-workshop/</guid><description>&lt;h2 id="keynote-talk">Keynote Talk&lt;/h2>
&lt;p>This is the keynote talk for the Sustainable Transport and Planning for Cleaner and Healthier Cities Workshop.&lt;/p>
&lt;p>&lt;a href="https://www.robinlovelace.net" target="_blank" rel="noopener">Robin Lovelace&lt;/a> is Professor of Transport Data Science at the Leeds Institute for Transport Studies and Head of Data Science at Active Travel England.&lt;/p></description></item><item><title>Inaugural Lecture: Data Science for Future-Proof Transport Planning</title><link>https://robinlovelace.net/old-site/talk/inaugural-lecture-data-science-for-future-proof-transport-planning/</link><pubDate>Thu, 08 May 2025 16:30:00 +0100</pubDate><guid>https://robinlovelace.net/old-site/talk/inaugural-lecture-data-science-for-future-proof-transport-planning/</guid><description>&lt;!-- ![demo](image.png) --></description></item><item><title>Cycle Route Uptake and Scenario Estimation (CRUSE): An Approach for Developing Strategic Cycle Network Planning Tools</title><link>https://robinlovelace.net/old-site/publication/lovelace-cycle-2024/</link><pubDate>Mon, 30 Sep 2024 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/publication/lovelace-cycle-2024/</guid><description/></item><item><title>Open Source Tools for Geographic Analysis in Transport Planning</title><link>https://robinlovelace.net/old-site/publication/lovelace-open-2021/</link><pubDate>Fri, 01 Oct 2021 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/publication/lovelace-open-2021/</guid><description/></item><item><title>Open Science In Transportation Workshop</title><link>https://robinlovelace.net/old-site/post/open-science-in-transportation-workshop/</link><pubDate>Thu, 21 Jan 2021 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/post/open-science-in-transportation-workshop/</guid><description>
&lt;script src="https://robinlovelace.net/old-site/post/open-science-in-transportation-workshop/index_files/header-attrs/header-attrs.js">&lt;/script>
&lt;p>Today I am presenting at the &lt;a href="http://www.trb.org/AnnualMeeting/AnnualMeeting.aspx">Transport Research Board’s 2021 Annual&lt;/a> meeting, aka &lt;a href="https://twitter.com/search?q=%23TRB2021">#TRB2021&lt;/a>.
I say ‘presenting’ but in fact, the talk was a pre-record - see the video below!&lt;/p>
&lt;iframe width="560" height="315" src="https://www.youtube.com/embed/mpqNW9ePVtw" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
&lt;/iframe>
&lt;p>Information on the talk can be found &lt;a href="https://www.robinlovelace.net/talk/open-access-transport-models-from-collaborative-code-to-citizen-science/">here&lt;/a>.&lt;/p>
&lt;p>It’s based on a recently published paper on ‘open access models’ which are both based on open source code and, vitally, are available for use by the public without needing specialist training &lt;span class="citation">(&lt;a href="#ref-lovelace_open_2020" role="doc-biblioref">Lovelace, Parkin, and Cohen 2020&lt;/a>)&lt;/span>.&lt;/p>
&lt;p>It also links to a just-published paper in which I review a range of open source transport planning tools &lt;span class="citation">(&lt;a href="#ref-lovelace_open_2021" role="doc-biblioref">Lovelace 2021&lt;/a>)&lt;/span>.&lt;/p>
&lt;p>See the interactive slides &lt;a href="https://www.robinlovelace.net/presentations/2021-01-21-open-source-transport-tools.html">here&lt;/a>.&lt;/p>
&lt;p>And &lt;a href="https://www.robinlovelace.net/talk/open-access-transport-models-from-collaborative-code-to-citizen-science/">here&lt;/a> for a link to the code, video and more!&lt;/p>
&lt;div id="references" class="section level1 unnumbered">
&lt;h1>References&lt;/h1>
&lt;div id="refs" class="references csl-bib-body hanging-indent">
&lt;div id="ref-lovelace_open_2021" class="csl-entry">
Lovelace, Robin. 2021. &lt;span>“Open Source Tools for Geographic Analysis in Transport Planning.”&lt;/span> &lt;em>Journal of Geographical Systems&lt;/em>, January. &lt;a href="https://doi.org/10.1007/s10109-020-00342-2">https://doi.org/10.1007/s10109-020-00342-2&lt;/a>.
&lt;/div>
&lt;div id="ref-lovelace_open_2020" class="csl-entry">
Lovelace, Robin, John Parkin, and Tom Cohen. 2020. &lt;span>“Open Access Transport Models: &lt;span>A&lt;/span> Leverage Point in Sustainable Transport Planning.”&lt;/span> &lt;em>Transport Policy&lt;/em> 97 (October): 47–54. &lt;a href="https://doi.org/10.1016/j.tranpol.2020.06.015">https://doi.org/10.1016/j.tranpol.2020.06.015&lt;/a>.
&lt;/div>
&lt;/div>
&lt;/div></description></item><item><title>R vs QGIS for sustainable transport planning</title><link>https://robinlovelace.net/old-site/post/2015-04-20-r-vs-qgis-for-transport/</link><pubDate>Tue, 20 Jan 2015 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/post/2015-04-20-r-vs-qgis-for-transport/</guid><description>&lt;p>The 23rd iteration of the GIS Research UK conference
(&lt;a href="https://github.com/Robinlovelace/stplanr" target="_blank" rel="noopener">#GISRUK&lt;/a>)
conference was the largest ever. 250 researchers,
industry representatives and academics attended from the
vibrant geospatial research communities in the
UK, Europe and beyond. GISRUK has become a centrepoint for discussion
of new methods, software and applications in the field. I
was on the &lt;a href="http://leeds.gisruk.org/contact.html" target="_blank" rel="noopener">organising committee&lt;/a>,
reviewed some excellent papers for the event
(a full list of these is
&lt;a href="http://leeds.gisruk.org/programme.html" target="_blank" rel="noopener">available for download here&lt;/a>) and
attended some truly ground-breaking talks.
This experience has shown that the geospatial community
in the UK is strong,
especially with regards to growth in open access
data and open source software in the field.&lt;/p>
&lt;p>&lt;a href="https://www.flickr.com/photos/97888609@N02/17025120518" title="img_2814 by Robin Lovelace, on Flickr">&lt;img src="https://farm6.staticflickr.com/5450/17025120518_ebfba47bff_s.jpg" width="75" height="75" alt="img_2814">&lt;/a>&lt;/p>
&lt;p>This article is about one part of GISRUK and insights gleaned from it
about R, QGIS and other tools for sustainable transport planning.
&lt;a href="https://github.com/Robinlovelace/GIS4TA" target="_blank" rel="noopener">GIS for Transport Applications&lt;/a>
(&lt;a href="https://twitter.com/hashtag/gis4ta?src=hash" target="_blank" rel="noopener">#GIS4TA for short&lt;/a>) was a practical day-long workshop
that preceded the main event.
I organised the workshop and (with help from
&lt;a href="http://www.geog.leeds.ac.uk/people/e.odiari" target="_blank" rel="noopener">Eusebio Odiari&lt;/a>,
&lt;a href="https://tgrg.wordpress.com/" target="_blank" rel="noopener">The Transport Geography Research Group&lt;/a>
and the
&lt;a href="http://www.rgs.org/HomePage.htm" target="_blank" rel="noopener">Royal Geographical Society&lt;/a>)
it seems to have been a great success. More than 30
people attended, including a decent portion
from transport consultancies such as
&lt;a href="http://www.itpworld.net/index.html" target="_blank" rel="noopener">Integrated Transport Planning Ltd&lt;/a>
&lt;a href="http://www.trpconsult.com/" target="_blank" rel="noopener">TRP Consulting&lt;/a> and the
&lt;a href="http://www.era.europa.eu/Pages/Home.aspx" target="_blank" rel="noopener">European Railway Association (ERA)&lt;/a>.
Specifically, it is about the use of R
and QGIS tools for transport planning and the potential for their
adoption in academic, public and private-sector transport planning.
The focus of the workshop was deliberately on open source software
and sustainable transport because these are growth
areas in the field that are essential for
democratic
and healthy
transport systems compatible with the
science of climate change (&lt;a href="http://www.opentraffic.net/en/">&lt;span class="citation">Tamminga, 2012&lt;/span>&lt;/a>).
A recent report, for example, suggests we need to almost completely
transition away from fossil fuels by 2050
(&lt;a href="http://dx.doi.org/10.1038/nature14016" target="_blank" rel="noopener">McGlade et al., 2015&lt;/a>).
New datasets and methods for analysing and modelling them can
help get us there in the recalcitrant transport sector
(&lt;a href="http://dx.doi.org/10.1016/j.jtrangeo.2014.07.010" target="_blank" rel="noopener">Gossling, 2014&lt;/a>).&lt;/p>
&lt;h1 id="r-for-transport-applications">R for transport applications&lt;/h1>
&lt;p>The workshop kicked-off with a short
talk on &amp;lsquo;R and QGIS for transport applications&amp;rsquo;,
which laid out some of the motivations for running the
workshop outlined above. Other than a few
&amp;rsquo;early adopters&amp;rsquo;, the transport modelling community
is generally conservative, based largely on mature
proprietary products such as
&lt;a href="http://www.saturnsoftware.co.uk/7.html" target="_blank" rel="noopener">SATURN&lt;/a>
and
&lt;a href="http://vision-traffic.ptvgroup.com/en-us/products/ptv-vissim/" target="_blank" rel="noopener">Vissim&lt;/a>.&lt;/p>
&lt;p>The slides from this talk are available here:&lt;/p>
&lt;script async class="speakerdeck-embed" data-id="91fce9cf5c36405b8969c6b6954c4fe6" data-ratio="1.41436464088398" src="//speakerdeck.com/assets/embed.js">&lt;/script>
&lt;p>&lt;strong>Tutorial: &lt;a href="https://github.com/Robinlovelace/GIS4TA/releases/download/0.1/intro-r-qgis-4ta.pdf" target="_blank" rel="noopener">Introduction to R and QGIS for transport applications&lt;/a>&lt;/strong>&lt;/p>
&lt;h1 id="routing-with-r">Routing with R&lt;/h1>
&lt;p>The second talk was by Nick Bearman, who provided an overview of
routing in R, as well as an excellent
&lt;a href="https://github.com/nickbearman/transport-workshop/blob/master/transport-workshop.pdf" target="_blank" rel="noopener">practical tutorial&lt;/a>.&lt;/p>
&lt;p>The practical demonstrated 2 ways of routing in R:&lt;/p>
&lt;ol>
&lt;li>Using &lt;strong>ggmap&lt;/strong>. The following code was used to navigate to the event!&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">from &amp;lt;- &amp;#39;Leeds station, New Station Street, Leeds LS1 5DL, United Kingdom&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">to &amp;lt;- &amp;#39;LS2 9JT&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">route_df &amp;lt;- route(from, to, structure = &amp;#39;route&amp;#39;, mode = &amp;#39;walking&amp;#39;)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">qmap(&amp;#39;Merrion Centre&amp;#39;, zoom = 15) +
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> geom_path(
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> aes(x = lon, y = lat), colour = &amp;#39;red&amp;#39;, size = 1.5,
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> data = route_df, lineend = &amp;#39;round&amp;#39;)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://github.com/Robinlovelace/robinlovelace.github.io/raw/master/_posts/writeup_files/figure-html/unnamed-chunk-2-1.png" alt="" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;ol start="2">
&lt;li>The &lt;a href="https://github.com/Robinlovelace/stplanr" target="_blank" rel="noopener">package I created, &lt;strong>stplanr&lt;/strong>&lt;/a>,
to get routes optimised for cyclists (see &lt;a href="https://github.com/nickbearman/transport-workshop/blob/master/transport-workshop.Rmd" target="_blank" rel="noopener">transport-workshop.Rmd&lt;/a> for a working version):&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">rquiet &amp;lt;- gLines2CyclePath(l = rlines, plan = &amp;#34;quietest&amp;#34;)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">plot(rquiet[1,]) # route from Leeds station to Leeds University (North - South)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">plot(rquiet[2,]) # route from Leeds to Manchester!
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://github.com/Robinlovelace/robinlovelace.github.io/raw/master/_posts/writeup_files/figure-html/unnamed-chunk-4-1.png" alt="" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>&lt;strong>Tutorial: &lt;a href="https://github.com/nickbearman/transport-workshop/blob/master/transport-workshop.pdf" target="_blank" rel="noopener">Route analysis using R&lt;/a>&lt;/strong>&lt;/p>
&lt;h1 id="large-gps-datasets-with-postgis">Large gps datasets with PostGIS&lt;/h1>
&lt;p>The most technical session involved using R to query huge datasets storing
GPS data containing 100,000+ rows. Amazingly, Richard and Adrian Ellison
set up a remotely accessible database instance &lt;strong>from their laptop&lt;/strong> which
participants queried via
&lt;a href="http://cran.r-project.org/web/packages/RPostgreSQL/index.html" target="_blank" rel="noopener">&lt;strong>RPostgreSQL&lt;/strong>&lt;/a>.
Their session information can be seen here:&lt;/p>
&lt;p>&lt;a href="https://github.com/richardellison/GIS4TA_GPS" target="_blank" rel="noopener">&lt;strong>github.com/richardellison/GIS4TA_GPS&lt;/strong>&lt;/a>&lt;/p>
&lt;h1 id="a-hackathon">A hackathon&lt;/h1>
&lt;p>Finally there was a miniature hackathon organised by Godwin Yeboah.
Participants made progress in better understanding the travel
patterns of cyclists using real data. The hackathon notes can be found here:&lt;/p>
&lt;p>&lt;a href="https://github.com/spatialscientist/GIS4TA2015" target="_blank" rel="noopener">github.com/spatialscientist/GIS4TA2015&lt;/a>&lt;/p>
&lt;h1 id="summary">Summary&lt;/h1>
&lt;p>GIS is a field of knowledge that has a huge amount to offer transport
planners and researchers, especially regarding new and open source
software tools that can effectively generate, process and analyse
transport-related data. R is well-suited to fill this research gap and
has a wide range of tools to help. Packages such as &lt;strong>ggmap&lt;/strong>
(Kahle and Wickham 2013),
&lt;strong>RPostgreSQL&lt;/strong>
and the new &lt;a href="https://github.com/Robinlovelace/stplanr" target="_blank" rel="noopener">&lt;strong>stplanr&lt;/strong>&lt;/a>
have great potential to help plan the transport systems of the future.
QGIS is also increasingly attractive for transport applications, with
it inbuilt support for
&lt;a href="http://planet.qgis.org/planet/tag/pgrouting/" target="_blank" rel="noopener">PGRouting&lt;/a>,
&lt;a href="http://plugins.qgis.org/plugins/FlowMapper/" target="_blank" rel="noopener">flow analysis&lt;/a>
and a friendly user interface that many will be used to.&lt;/p>
&lt;p>Photos taken during the hackathon are testament to its role as a forum
for not only learning but also debate about the future of GIS in transport.
These can be seen here:&lt;/p>
&lt;p>&lt;a href="https://www.flickr.com/photos/97888609@N02/sets/72157652012715766" target="_blank" rel="noopener">flickr.com/photos/97888609@N02/sets/72157652012715766&lt;/a>&lt;/p>
&lt;p>Hearing feedback from users new to R using it to solve transport problems
provided an insight into how it compares to traditional tools. The removal
of &amp;lsquo;glass ceilings&amp;rsquo; imposed by restrictive licenses or the need to buy
&amp;lsquo;add-on&amp;rsquo; features was one comment, but that applies equally to QGIS and
other &lt;a href="http://foss4g.org/" target="_blank" rel="noopener">FOSS4G&lt;/a>
offerings. The steep learning curve of R seems to still
be an issue compared with QGIS, although this is becoming less of an issue
with the evolution of RStudio as an GUI for R. In conclusion, both R and
QGIS are coming of age as tools in the transport planner&amp;rsquo;s &amp;lsquo;war cabinet&amp;rsquo;.
The latest evidence unequivocally shows the impact of transport decisions
on obesity, environmental degradation and quality of life. So it is time,
surely, to harness this new open source software to &amp;lsquo;save the world&amp;rsquo;!&lt;/p>
&lt;h1 id="acknowledgements">Acknowledgements&lt;/h1>
&lt;p>Thanks to the Consumer Data Research Centre and the Royal Geographical
society for subsidising the event. Thanks to all the participants and
especially the demonstrators Godwin, Nick, Adrian and Richard for making it
happen.&lt;/p>
&lt;h1 id="references">References&lt;/h1>
&lt;p>Gössling, Stefan, and Scott Cohen. 2014. “Why sustainable transport policies will fail: EU climate policy in the light of transport taboos.” Journal of Transport Geography 39 (July). Elsevier Ltd: 197–207. doi:10.1016/j.jtrangeo.2014.07.010.&lt;/p>
&lt;p>Kahle, D, and Hadley Wickham. 2013. “ggmap: Spatial Visualization with ggplot2.” The R Journal 5: 144–61. &lt;a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.375.8693&amp;amp;rep=rep1&amp;amp;type=pdf" target="_blank" rel="noopener">citeseerx.ist.psu.edu&lt;/a>.&lt;/p>
&lt;p>McGlade, Christophe, and Paul Ekins. 2015. “The geographical distribution of fossil fuels unused when limiting global warming to 2 °C.” Nature 517 (7533). Nature Publishing Group: 187–90. doi:10.1038/nature14016.&lt;/p>
&lt;p>Tamminga, Guus, Marc Miska, Edgar Santos, Hans van Lint, Arturo Nakasone, Helmut Prendinger, and Serge Hoogendoorn. 2012. “Design of Open Source Framework for Traffic and Travel Simulation.” Transportation Research Record: Journal of the Transportation Research Board 2291 (-1): 44–52. doi:10.3141/2291-06.&lt;/p></description></item><item><title>Clipping spatial data in R</title><link>https://robinlovelace.net/old-site/post/2014-07-29-clipping-with-r/</link><pubDate>Tue, 29 Jul 2014 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/post/2014-07-29-clipping-with-r/</guid><description>&lt;h1 id="heading">&lt;/h1>
&lt;p>This miniature vignette shows how to clip spatial data based on different spatial objects in R and a &amp;lsquo;bounding box&amp;rsquo;. Spatial overlays are common in GIS applications and R users are fortunate that the clipping and spatial subsetting functions are mature and fairly fast. We&amp;rsquo;ll also write a new function called &lt;code>gClip()&lt;/code>, that will make clipping by bounding boxes easier.&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/Westminster.png" alt="plot of chunk Westminster" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h2 id="loading-the-data">Loading the data&lt;/h2>
&lt;p>To start with let&amp;rsquo;s load some data. I&amp;rsquo;m working in the root directory of the &lt;a href="https://github.com/Robinlovelace/Creating-maps-in-R" target="_blank" rel="noopener">Creating-maps-in-R&lt;/a> github repository, so there are some spatial datasets available to play with:&lt;/p>
&lt;pre>&lt;code>setwd(&amp;quot;../&amp;quot;)
library(rgdal)
stations &amp;lt;- readOGR(&amp;quot;data&amp;quot;, &amp;quot;lnd-stns&amp;quot;)
## OGR data source with driver: ESRI Shapefile
## Source: &amp;quot;data&amp;quot;, layer: &amp;quot;lnd-stns&amp;quot;
## with 2532 features and 27 fields
## Feature type: wkbPoint with 2 dimensions
zones &amp;lt;- readOGR(&amp;quot;data&amp;quot;, &amp;quot;london_sport&amp;quot;)
## OGR data source with driver: ESRI Shapefile
## Source: &amp;quot;data&amp;quot;, layer: &amp;quot;london_sport&amp;quot;
## with 33 features and 4 fields
## Feature type: wkbPolygon with 2 dimensions
&lt;/code>&lt;/pre>
&lt;h2 id="the-wonder-of-spatial-subsetting-in-r">The wonder of spatial subsetting in R&lt;/h2>
&lt;p>Now, it&amp;rsquo;s easy to &lt;strong>subset&lt;/strong> spatial data in R, using the same incredibly concise square bracket &lt;code>[]&lt;/code> notation as R uses for non spatial data. To re-confirm how this works on non-spatial data, here&amp;rsquo;s a mini example:&lt;/p>
&lt;pre>&lt;code>M &amp;lt;- matrix(1:10, ncol = 5)
M[2, 3:5]
## [1] 6 8 10
&lt;/code>&lt;/pre>
&lt;p>The above code creates a matrix with 5 columns and 2 rows: the &lt;code>[2, 3:5]&lt;/code> part takes the subset of &lt;code>M&lt;/code> corresponding to 3rd to 5th columns in the second row.&lt;/p>
&lt;p>Subsetting spatial data works in exactly the same way: note that &lt;code>zones&lt;/code> are far more extensive than the &lt;code>stations&lt;/code> points. (We have to change the projection of &lt;code>stations&lt;/code> before plotting, so the objects are on the same coordinate reference system.)&lt;/p>
&lt;pre>&lt;code>stations &amp;lt;- spTransform(stations, CRS(proj4string(zones))) # transform CRS
plot(zones)
points(stations)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/unnamed-chunk-3.png" alt="plot of chunk unnamed-chunk-3" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>So how do we select only points that are are within the zones of London? Believe it or not, it&amp;rsquo;s as simple as subsetting the matrix &lt;code>M&lt;/code> above. This is an amazingly concise and convenient way of spatial subsetting that was added to R at some point between versions 1 and 2 of &lt;a href="http://www.asdar-book.org/" target="_blank" rel="noopener">Applied Spatial Data Analysis with R&lt;/a>. In the earlier (2008) book, one had to use &lt;code>overlay(x, y)&lt;/code> just to get the selection, and then another line of code was required to actually make the subset. Now things are much simpler. As Bivand et al. put it in the &lt;a href="http://www.springer.com/statistics/life&amp;#43;sciences,&amp;#43;medicine&amp;#43;%26&amp;#43;health/book/978-1-4614-7617-7" target="_blank" rel="noopener">latest edtion&lt;/a> (p. 131), &amp;ldquo;the selection syntax for features was extended such that it understands:&amp;rdquo;&lt;/p>
&lt;pre>&lt;code>stations_subset &amp;lt;- stations[zones, ]
&lt;/code>&lt;/pre>
&lt;p>&lt;strong>This is so amazing and intuitive, whoever invented it should be given a medal!!&lt;/strong> Despite this simplicity, it seems many R users who I&amp;rsquo;ve taught spatial functions to are unaware of &lt;code>[]&lt;/code>&amp;rsquo;s spatial extension. So spread the word (and if anyone knows of the history of this innovation, please let us know). Now we have a sample of all stations zones: progress.&lt;/p>
&lt;pre>&lt;code>plot(zones)
points(stations_subset)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/unnamed-chunk-5.png" alt="plot of chunk unnamed-chunk-5" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h2 id="clipping-by-a-bounding-box">Clipping by a bounding box&lt;/h2>
&lt;p>But what if we want to &lt;em>clip&lt;/em> the polygons data, based on a bounding box? To start with, let&amp;rsquo;s look at and modify the existing bounding box for the zones, making it half the size:&lt;/p>
&lt;pre>&lt;code>b &amp;lt;- bbox(zones)
b[1, ] &amp;lt;- (b[1, ] - mean(b[1, ])) * 0.5 + mean(b[1, ])
b[2, ] &amp;lt;- (b[2, ] - mean(b[2, ])) * 0.5 + mean(b[2, ])
b &amp;lt;- bbox(t(b))
plot(zones, xlim = b[1, ], ylim = b[2, ])
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/unnamed-chunk-6.png" alt="plot of chunk unnamed-chunk-6" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>Now, to clip this area, we can use a custom function, which I&amp;rsquo;ve called &lt;code>gClip&lt;/code>, following the &lt;strong>rgeos&lt;/strong> function naming convention (this was inspired by an online &lt;a href="http://stackoverflow.com/questions/21883683/is-it-possible-to-clip-a-polygon-to-the-bounding-box-of-a-base-map" target="_blank" rel="noopener">answer&lt;/a> that didn&amp;rsquo;t work for me):&lt;/p>
&lt;pre>&lt;code>library(raster)
library(rgeos)
## rgeos version: 0.3-5, (SVN revision 447)
## GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921
## Polygon checking: TRUE
gClip &amp;lt;- function(shp, bb){
if(class(bb) == &amp;quot;matrix&amp;quot;) b_poly &amp;lt;- as(extent(as.vector(t(bb))), &amp;quot;SpatialPolygons&amp;quot;)
else b_poly &amp;lt;- as(extent(bb), &amp;quot;SpatialPolygons&amp;quot;)
gIntersection(shp, b_poly, byid = T)
}
zones_clipped &amp;lt;- gClip(zones, b)
## Warning: spgeom1 and spgeom2 have different proj4 strings
plot(zones_clipped)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/unnamed-chunk-7.png" alt="plot of chunk unnamed-chunk-7" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>Note that due to the &lt;code>if&lt;/code> statements in &lt;code>gClip&lt;/code>&amp;rsquo;s body, it can handle almost any spatial data input, and still work. Let&amp;rsquo;s clip to the borough of Westminster, one of London&amp;rsquo;s better known boroughs:&lt;/p>
&lt;pre>&lt;code>westminster &amp;lt;- zones[grep(&amp;quot;West&amp;quot;, zones$name),]
zones_clipped_w &amp;lt;- gClip(zones, westminster)
## Warning: spgeom1 and spgeom2 have different proj4 strings
plot(zones_clipped_w); plot(westminster, col = &amp;quot;red&amp;quot;, add = T)
&lt;/code>&lt;/pre>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/Robinlovelace/Creating-maps-in-R/master/vignettes/clipping-with-bounding-box_files/figure-markdown_github/Westminster.png" alt="plot of chunk Westminster" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>There are many more spatial tips available from the &lt;a href="https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf" target="_blank" rel="noopener">Introduction to visualising
spatial data in
R&lt;/a>
tutorial that &lt;a href="http://spatial.ly/" target="_blank" rel="noopener">James Cheshire&lt;/a> and I are maintaining. The source code of this post can also be viewed &lt;a href="https://github.com/Robinlovelace/Creating-maps-in-R/blob/master/vignettes/clipping-with-bounding-box.Rmd" target="_blank" rel="noopener">online&lt;/a> as just one of a series of &lt;a href="https://github.com/Robinlovelace/Creating-maps-in-R/tree/master/vignettes" target="_blank" rel="noopener">vignettes&lt;/a> to showcase some of R&amp;rsquo;s impressive spatial capabilities.&lt;/p>
&lt;h2 id="update">Update&lt;/h2>
&lt;p>There was a lively discussion of this post when it was first published in 2016.
Although I&amp;rsquo;ve switched to a new commenting system, the comments, which include more history and a link to the commit that added spatial subsetting to the sp package, can be found here: &lt;a href="https://disqus.com/home/discussion/robinlovelace/clipping_spatial_data_in_r/" target="_blank" rel="noopener">https://disqus.com/home/discussion/robinlovelace/clipping_spatial_data_in_r/&lt;/a>&lt;/p></description></item><item><title>Coxcomb plots and 'spiecharts' in R</title><link>https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-r/</link><pubDate>Fri, 27 Dec 2013 00:00:00 +0000</pubDate><guid>https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-r/</guid><description>&lt;p>After switching to a new site I decided to revive some old posts.
I found this one that was written back 7 years ago (in January 2021 when this update was written), back in December 2013.
The results of the book are now published in the book &lt;a href="https://www.routledge.com/Low-Impact-Living-A-Field-Guide-to-Ecological-Affordable-Community-Building/Chatterton/p/book/9780415661614">Low Impact Living: A Field Guide to Ecological, Affordable Community Building&lt;/a> (Chatterton, 2015, for more info on the Lilac project in particular and cohousing in general see &lt;a href="https://www.lilac.coop/resources/">lilac.coop/resources/&lt;/a>).
I was amazed to find that, with some tweaks, the &lt;code>ggplot2&lt;/code> code still ran.&lt;/p>
&lt;p>I was contacted recently by a housing organisation who wanted
an attractive visualisation of their finances, arranged in a circular
form. Because there were two 4 continuous variables to include, all
of which were proportions of each other, the client suggested a plot
similar to a pie chart, but with each segment extending out a different
radius from the segment. I realised later that what I had been asked to
make was a modified &lt;a href="http://en.wikipedia.org/wiki/Coxcomb_diagram#Polar_area_diagram">coxcomb&lt;/a>
plot, invented by
&lt;a href="http://en.wikipedia.org/wiki/Florence_Nightingale">Florence Nightingale&lt;/a>
to represent statistics on cause of death during the Crimean War.
In fact, I had been asked to make a “&lt;a href="http://www.cs.huji.ac.il/~feit/papers/Spie03TR.pdf">spie chart&lt;/a>.”
This post demonstrates, for the first time to my knowledge, how it can be done
using ggplot2. A reproducible example of this, including sample data input, can be
found on the project’s github repository: &lt;a href="https://github.com/Robinlovelace/lilacPlot" class="uri">https://github.com/Robinlovelace/lilacPlot&lt;/a> . Please fork and attribute as appropriate!&lt;/p>
&lt;div id="reading-and-looking-at-the-data" class="section level2">
&lt;h2>Reading and looking at the data&lt;/h2>
&lt;p>This is the original dataset I was given:&lt;/p>
&lt;pre class="r">&lt;code>u &amp;lt;- &amp;quot;https://github.com/Robinlovelace/lilacPlot/raw/master/F2.csv&amp;quot;
f &amp;lt;- read.csv(u)
knitr::kable(f[1:3, ])&lt;/code>&lt;/pre>
&lt;table>
&lt;thead>
&lt;tr class="header">
&lt;th align="left">H&lt;/th>
&lt;th align="right">Value&lt;/th>
&lt;th align="right">Value.P&lt;/th>
&lt;th align="right">Allocation&lt;/th>
&lt;th align="right">Deposit&lt;/th>
&lt;th align="right">Captial&lt;/th>
&lt;th align="right">Debt&lt;/th>
&lt;th align="right">Cap&lt;/th>
&lt;th align="right">Contribution&lt;/th>
&lt;th align="right">Repayments&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr class="odd">
&lt;td align="left">q&lt;/td>
&lt;td align="right">163827&lt;/td>
&lt;td align="right">0.065&lt;/td>
&lt;td align="right">0.979&lt;/td>
&lt;td align="right">16382&lt;/td>
&lt;td align="right">147445&lt;/td>
&lt;td align="right">0&lt;/td>
&lt;td align="right">2457.405&lt;/td>
&lt;td align="right">1287.24&lt;/td>
&lt;td align="right">0.00&lt;/td>
&lt;/tr>
&lt;tr class="even">
&lt;td align="left">a&lt;/td>
&lt;td align="right">165994&lt;/td>
&lt;td align="right">0.066&lt;/td>
&lt;td align="right">1.022&lt;/td>
&lt;td align="right">16599&lt;/td>
&lt;td align="right">5488&lt;/td>
&lt;td align="right">138847&lt;/td>
&lt;td align="right">2489.910&lt;/td>
&lt;td align="right">208.02&lt;/td>
&lt;td align="right">208.02&lt;/td>
&lt;/tr>
&lt;tr class="odd">
&lt;td align="left">z&lt;/td>
&lt;td align="right">159425&lt;/td>
&lt;td align="right">0.063&lt;/td>
&lt;td align="right">0.933&lt;/td>
&lt;td align="right">15943&lt;/td>
&lt;td align="right">76632&lt;/td>
&lt;td align="right">63601&lt;/td>
&lt;td align="right">2391.375&lt;/td>
&lt;td align="right">995.46&lt;/td>
&lt;td align="right">995.46&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Without worrying too much about the details, the basics of the dataset are
as follows:&lt;/p>
&lt;ul>
&lt;li>One observation per row, these will later be bars on the box plot&lt;/li>
&lt;li>Two components of data - captital and revenue&lt;/li>
&lt;li>Different orders of magnitude: some data is in absolute monetary terms, some in percentages&lt;/li>
&lt;/ul>
&lt;p>Base on the above points, a prerequisite was to create preliminary plots and manipulate the
data so it would better fit in a coxcomb plot.&lt;/p>
&lt;p>The first stage, however, is to demonstrate how the addition of
&lt;code>coord_polar&lt;/code> to a barchart can conver it into a pie chart:&lt;/p>
&lt;pre class="r">&lt;code>library(ggplot2)
(p &amp;lt;- ggplot(f, aes(x = H, y = Allocation)) + geom_bar(color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;,
width = 1))&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-2-1.png" width="672" />&lt;/p>
&lt;div class="figure">
&lt;img src="" alt="" />
&lt;p class="caption">plot of chunk unnamed-chunk-2&lt;/p>
&lt;/div>
&lt;pre class="r">&lt;code>p + coord_polar()&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-3-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-2](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-22.png) -->
&lt;p>The above example works well, but notice that all the bars are of equal widths.
What we want is to be proportional to a value (variable “Value”) of each observation.
To do this we use the age-old function &lt;code>cumsum&lt;/code>, as described in an
answer to a &lt;a href="http://stackoverflow.com/questions/20688376/how-to-make-variable-bar-widths-in-ggplot2-not-overlap-or-gap">stackexchange question&lt;/a>.&lt;/p>
&lt;pre class="r">&lt;code>w &amp;lt;- f$Value
pos &amp;lt;- 0.5 * (cumsum(w) + cumsum(c(0, w[-length(w)])))
(p &amp;lt;- ggplot(f, aes(x = pos)) + geom_bar(aes(y = Allocation), width = w, color = &amp;quot;black&amp;quot;,
stat = &amp;quot;identity&amp;quot;))&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-4-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-3](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-31.png) -->
&lt;pre class="r">&lt;code>p + coord_polar(theta = &amp;quot;x&amp;quot;) + scale_x_continuous(labels = f$H, breaks = pos)&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-5-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-3](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-32.png) -->
&lt;p>Finally a spie chart has been created. After that revelation, it was essentially about adding the ‘bells and
whistles’, including a 10% line to represent how much more or less than their share each observation was
paying.&lt;/p>
&lt;/div>
&lt;div id="adding-the-10" class="section level2">
&lt;h2>Adding the 10 %&lt;/h2>
&lt;pre class="r">&lt;code>f$Deposit/f$Value&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## [1] 0.09999573 0.09999759 0.10000314 0.09999837 0.10000120 0.10000000
## [7] 0.10000311 0.10000085 0.10000511 0.10000356 0.09999676 0.09999700
## [13] 0.09999812 0.10000511 0.10000085 0.10000240 0.10000000 0.10000694
## [19] 0.09999901 0.09999883&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code># add 10% in there
p &amp;lt;- ggplot(f)
p + geom_bar(aes(x = pos, y = Allocation), width = w, color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;) +
geom_bar(aes(x = pos, y = 0.1), width = w, color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;green&amp;quot;) + coord_polar()&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-7-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-4](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-41.png) -->
&lt;pre class="r">&lt;code># make proportional to area
f$Allo &amp;lt;- sqrt(f$Allocation)
p &amp;lt;- ggplot(f)
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;) +
geom_bar(aes(x = pos, y = sqrt(0.1), width = w), color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;green&amp;quot;) + coord_polar()&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-8-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-4](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-42.png) -->
&lt;pre class="r">&lt;code># add capital
capital &amp;lt;- (f$Captial + f$Deposit)/(f$Value) * f$Allocation
capital &amp;lt;- sqrt(capital)
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;) +
geom_bar(aes(x = pos, y = capital, width = w), color = &amp;quot;black&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;red&amp;quot;) + geom_bar(aes(x = pos, y = sqrt(0.1), width = w), color = &amp;quot;black&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;green&amp;quot;) + coord_polar() + scale_x_continuous(labels = f$H,
breaks = pos)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-9-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-4](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-43.png) -->
&lt;pre class="r">&lt;code># add ablines
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;lightgrey&amp;quot;) + geom_bar(aes(x = pos, y = capital, width = w), color = &amp;quot;grey40&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;red&amp;quot;) + geom_bar(aes(x = pos, y = sqrt(0.1),
width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;green&amp;quot;) + geom_abline(intercept = 1,
slope = 0, linetype = 2) + geom_abline(intercept = sqrt(1.1), slope = 0,
linetype = 3) + geom_abline(intercept = sqrt(0.9), slope = 0, linetype = 3)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-10-1.png" width="672" />&lt;/p>
&lt;div class="figure">
&lt;img src="https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-44.png" alt="" />
&lt;p class="caption">plot of chunk unnamed-chunk-4&lt;/p>
&lt;/div>
&lt;pre class="r">&lt;code># calculate vertical ablines of divisions
v1 &amp;lt;- 0.51 * f$Value[1]
v2 &amp;lt;- cumsum(f$Value)[17] + f$Value[18] * 0.31
v3 &amp;lt;- cumsum(f$Value)[17] + f$Value[18] * 0.64
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;lightgrey&amp;quot;) +
geom_vline(x = v1, linetype = 5, xintercept = 0) +
geom_vline(x = v2, linetype = 5, xintercept = 0) +
geom_vline(x = v3, linetype = 5, xintercept = 0) +
coord_polar()&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown parameters: x
## Warning: Ignoring unknown parameters: x
## Warning: Ignoring unknown parameters: x&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-11-1.png" width="672" />&lt;/p>
&lt;pre class="r">&lt;code># putting it all together
p &amp;lt;- ggplot(f)
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;lightgrey&amp;quot;) + geom_bar(aes(x = pos, y = capital, width = w), color = &amp;quot;grey40&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;red&amp;quot;) + geom_bar(aes(x = pos, y = sqrt(0.1),
width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;green&amp;quot;) + geom_abline(intercept = 1,
slope = 0, linetype = 2) + geom_abline(intercept = sqrt(1.1), slope = 0,
linetype = 3) + geom_abline(intercept = sqrt(0.9), slope = 0, linetype = 3) +
geom_vline(x = v1, linetype = 5, xintercept = 0) +
geom_vline(x = v2, linetype = 5, xintercept = 0) +
geom_vline(x = v3, linetype = 5, xintercept = 0) +
coord_polar() + scale_x_continuous(labels = f$H, breaks = pos) +
theme_classic()&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown parameters: x
## Warning: Ignoring unknown parameters: x
## Warning: Ignoring unknown parameters: x&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-12-1.png" width="672" />&lt;/p>
&lt;p>The above looks great, but ideally, for an ‘infographic’ feel, it would
have no annoying axes clogging up the visuals. This was done by creating an
entirely new ggpot theme.&lt;/p>
&lt;/div>
&lt;div id="create-theme-with-no-axes" class="section level2">
&lt;h2>Create theme with no axes&lt;/h2>
&lt;pre class="r">&lt;code>theme_infog &amp;lt;- theme_classic() + theme(axis.line = element_blank(), axis.title = element_blank(),
axis.ticks = element_blank(), axis.text.y = element_blank())
last_plot() + theme_infog&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-13-1.png" width="672" />&lt;/p>
&lt;/div>
&lt;div id="creating-a-ring" class="section level2">
&lt;h2>Creating a ring&lt;/h2>
&lt;p>To add the revenue element to the graph is not a task to be taken likely.
This was how I tackled the problem, by creating a tall, variable-width
bar chart first, and later adding the original spie chart after:&lt;/p>
&lt;pre class="r">&lt;code>f$Cap.r &amp;lt;- f$Cap/mean(f$Cap) * 0.1 + 1.2
f$Cont.r &amp;lt;- f$Contribution/mean(f$Cap) * 0.1 + 1.2
f$Rep.r &amp;lt;- f$Cont.r + f$Repayments/mean(f$Cap) * 0.1
f$H &amp;lt;- c(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;f&amp;quot;, &amp;quot;g&amp;quot;, &amp;quot;h&amp;quot;, &amp;quot;i&amp;quot;, &amp;quot;j&amp;quot;, &amp;quot;k&amp;quot;, &amp;quot;l&amp;quot;, &amp;quot;m&amp;quot;, &amp;quot;n&amp;quot;,
&amp;quot;o&amp;quot;, &amp;quot;p&amp;quot;, &amp;quot;q&amp;quot;, &amp;quot;r&amp;quot;, &amp;quot;s&amp;quot;, &amp;quot;t&amp;quot;)
p &amp;lt;- ggplot(f)
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;lightgrey&amp;quot;)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-14-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-6](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-61.png) -->
&lt;pre class="r">&lt;code># we need the axes to be bigger for starters - try 1.3 to 1.5
p + geom_bar(aes(x = pos, y = Cap.r, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;white&amp;quot;) + geom_bar(aes(x = pos, y = Rep.r, width = w), color = &amp;quot;grey40&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;grey80&amp;quot;) + geom_bar(aes(x = pos, y = Cont.r,
width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;grey30&amp;quot;) + geom_bar(aes(x = pos,
y = 1.196, width = w), color = &amp;quot;white&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;white&amp;quot;)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-15-1.png" width="672" />&lt;/p>
&lt;pre class="r">&lt;code>last_plot() + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;grey80&amp;quot;) + geom_bar(aes(x = pos, y = capital,
width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;grey30&amp;quot;) + geom_bar(aes(x = pos,
y = sqrt(0.1), width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;black&amp;quot;) +
geom_abline(intercept = 1, slope = 0, linetype = 5) + geom_abline(intercept = sqrt(1.1),
slope = 0, linetype = 3) + geom_abline(intercept = sqrt(0.9), slope = 0,
linetype = 3) + coord_polar() + scale_x_continuous(labels = f$H, breaks = pos) +
theme_infog&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-16-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-6](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-63.png) -->
&lt;/div>
&lt;div id="just-inner" class="section level2">
&lt;h2>Just inner&lt;/h2>
&lt;p>After all that it was decided it looked nicer with only the inner ring anyway.
Here is the finished product:&lt;/p>
&lt;pre class="r">&lt;code>p &amp;lt;- ggplot(f)
p + geom_bar(aes(x = pos, y = Allo, width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;,
fill = &amp;quot;grey80&amp;quot;) + geom_bar(aes(x = pos, y = capital, width = w), color = &amp;quot;grey40&amp;quot;,
stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;grey30&amp;quot;) + geom_bar(aes(x = pos, y = sqrt(0.1),
width = w), color = &amp;quot;grey40&amp;quot;, stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;black&amp;quot;) + geom_abline(intercept = 1,
slope = 0, linetype = 5) + geom_abline(intercept = sqrt(1.1), slope = 0,
linetype = 3) + geom_abline(intercept = sqrt(0.9), slope = 0, linetype = 3) +
coord_polar() + scale_x_continuous(labels = f$H, breaks = pos) + theme_infog&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width
## Warning: Ignoring unknown aesthetics: width&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://robinlovelace.net/old-site/post/2013-12-27-coxcomb-plots-spiecharts-R_files/figure-html/unnamed-chunk-17-1.png" width="672" />&lt;/p>
&lt;!-- ![plot of chunk unnamed-chunk-7](https://raw.github.com/Robinlovelace/robinlovelace.github.io/master/figure/unnamed-chunk-7.png) -->
&lt;pre class="r">&lt;code>ggsave(&amp;quot;just-inner.png&amp;quot;, width = 7, height = 7, dpi = 800)&lt;/code>&lt;/pre>
&lt;/div></description></item></channel></rss>