<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/using-r-as-a-gis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://realdataweb.wordpress.com/2017/04/22/using-r-as-a-gis/"> R – Real Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="1" class="wp-image-510" src="https://realdataweb.files.wordpress.com/2017/04/1.jpg?w=456" data-recalc-dims="1"></p>
<p>In real estate, spatial data is the name of the game. Countless programs<br>
in other domains utilize the power of this data, which is becoming more<br>
prevalent by the day.</p>
<p>In this post I will go over a few simple, but powerful tools to get you<br>
started using using geographic information in R.</p>
<pre>
##First, some libraries##
#install.packages('GISTools', dependencies = T)
library(GISTools)
</pre>
<p><code>GISTools</code> provides an easy-to-use method for creating shading schemes<br>
and choropleth maps. Some of you may have heard of the <code>sp</code> package,<br>
which adds numerous spatial classes to the mix. There are also functions<br>
for analysis and making things look nice.</p>
<p>Let’s get rolling: source the <code>vulgaris</code> dataset, which contains<br>
location information for Syringa Vulgaris (the Lilac) observation<br>
stations and US states. This code plots the states and <code>vulgaris</code><br>
points.</p>
<pre>
data(&amp;quot;vulgaris&amp;quot;) #load data
par = (mar = c(2,0,0,0)) #set margins of plot area
plot(us_states)
plot(vulgaris, add = T, pch = 20)
</pre>
<p><img title="Plot1" alt="alt text" src="https://i2.wp.com/thepracticaldev.s3.amazonaws.com/i/23vc4dmomjg5xns25ygk.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>One thing to note here is the structure of these objects. <code>us_states</code> is<br>
a SpatialPolygonsDataFrame, which stores information for plotting shapes<br>
(like a shapefile) within its attributes. <code>vulgaris</code> by contrast is a<br>
SpatialPointsDataFrame, which contains data for plotting individual<br>
points. Much like a <code>data.frame</code> and <code>$</code>, these objects harbor<br>
information that can be accessed via <code>@</code>.</p>
<pre>
kable(head(vulgaris@data))
</pre>
<table>
<thead>
<tr>
<th></th>
<th align="right">Station</th>
<th align="right">Year</th>
<th align="left">Type</th>
<th align="right">Leaf</th>
<th align="right">Bloom</th>
<th align="left">Station.Name</th>
<th align="left">State.Prov</th>
<th align="right">Lat</th>
<th align="right">Long</th>
<th align="right">Elev</th>
</tr>
</thead>
<tbody>
<tr>
<td>3695</td>
<td align="right">61689</td>
<td align="right">1965</td>
<td align="left">Vulgaris</td>
<td align="right">114</td>
<td align="right">136</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
<tr>
<td>3696</td>
<td align="right">61689</td>
<td align="right">1966</td>
<td align="left">Vulgaris</td>
<td align="right">122</td>
<td align="right">146</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
<tr>
<td>3697</td>
<td align="right">61689</td>
<td align="right">1967</td>
<td align="left">Vulgaris</td>
<td align="right">104</td>
<td align="right">156</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
<tr>
<td>3698</td>
<td align="right">61689</td>
<td align="right">1968</td>
<td align="left">Vulgaris</td>
<td align="right">97</td>
<td align="right">134</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
<tr>
<td>3699</td>
<td align="right">61689</td>
<td align="right">1969</td>
<td align="left">Vulgaris</td>
<td align="right">114</td>
<td align="right">138</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
<tr>
<td>3700</td>
<td align="right">61689</td>
<td align="right">1970</td>
<td align="left">Vulgaris</td>
<td align="right">111</td>
<td align="right">135</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
</tr>
</tbody>
</table>
<p>Let’s take a look at some functions that use this data.</p>
<pre>
newVulgaris kable(head(data.frame(newVulgaris)))
</pre>
<table>
<thead>
<tr>
<th></th>
<th align="right">x</th>
<th align="right">y</th>
</tr>
</thead>
<tbody>
<tr>
<td>3 4896</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
<tr>
<td>3 4897</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
<tr>
<td>3 4898</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
<tr>
<td>3 4899</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
<tr>
<td>3 4900</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
<tr>
<td>3 4901</td>
<td align="right">-67.65</td>
<td align="right">44.65</td>
</tr>
</tbody>
</table>
<p><code>gIntersection</code>, as you may have guessed from the name, returns the<br>
intersection of two spatial objects. In this case, we are given the<br>
points from <code>vulgaris</code> that are within <code>us_states</code>. However, the rest of<br>
the <code>vulgaris</code> data has been stripped from the resulting object. We’ve<br>
got to jump through a couple of hoops to get that information back.</p>
<pre>
&amp;lt;br /&amp;gt;newVulgaris &amp;lt;- data.frame(newVulgaris)
tmp &amp;lt;- rownames(newVulgaris)
tmp &amp;lt;- strsplit(tmp, &amp;quot; &amp;quot;)
tmp &amp;lt;- (sapply(tmp, &amp;quot;[[&amp;quot;, 2))
tmp &amp;lt;- as.numeric(tmp)
vdf &amp;lt;- data.frame(vulgaris)
newVulgaris &amp;lt;- subset(vdf, row.names(vdf) %in% tmp)
</pre>
<table>
<thead>
<tr>
<th></th>
<th align="right">Station</th>
<th align="right">Year</th>
<th align="left">Type</th>
<th align="right">Leaf</th>
<th align="right">Bloom</th>
<th align="left">Station.Name</th>
<th align="left">State.Prov</th>
<th align="right">Lat</th>
<th align="right">Long</th>
<th align="right">Elev</th>
<th align="right">Long.1</th>
<th align="right">Lat.1</th>
<th align="left">optional</th>
</tr>
</thead>
<tbody>
<tr>
<td>3695</td>
<td align="right">61689</td>
<td align="right">1965</td>
<td align="left">Vulgaris</td>
<td align="right">114</td>
<td align="right">136</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
<tr>
<td>3696</td>
<td align="right">61689</td>
<td align="right">1966</td>
<td align="left">Vulgaris</td>
<td align="right">122</td>
<td align="right">146</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
<tr>
<td>3697</td>
<td align="right">61689</td>
<td align="right">1967</td>
<td align="left">Vulgaris</td>
<td align="right">104</td>
<td align="right">156</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
<tr>
<td>3698</td>
<td align="right">61689</td>
<td align="right">1968</td>
<td align="left">Vulgaris</td>
<td align="right">97</td>
<td align="right">134</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
<tr>
<td>3699</td>
<td align="right">61689</td>
<td align="right">1969</td>
<td align="left">Vulgaris</td>
<td align="right">114</td>
<td align="right">138</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
<tr>
<td>3700</td>
<td align="right">61689</td>
<td align="right">1970</td>
<td align="left">Vulgaris</td>
<td align="right">111</td>
<td align="right">135</td>
<td align="left">COVENTRY</td>
<td align="left">CT</td>
<td align="right">41.8</td>
<td align="right">-72.35</td>
<td align="right">146</td>
<td align="right">-72.35</td>
<td align="right">41.8</td>
<td align="left">TRUE</td>
</tr>
</tbody>
</table>
<p>Look familiar? Now we’ve got a data frame with the clipped <code>vulgaris</code><br>
values and original data preserved.</p>
<pre>
vulgarisSpatial ```

After storing our clipped data frame as a SpatialPointsDataFrame, we can
again make use of it - in this case we add a shading scheme to the
`vulgaris` points.

``` r
shades shades$cols plot(us_states)
choropleth(vulgarisSpatial, vulgarisSpatial$Elev,shading = shades, add = T, pch = 20)
</pre>
<p><img title="Plot2" alt="alt text" src="https://i0.wp.com/thepracticaldev.s3.amazonaws.com/i/l6hm6mnpkdlc1yo04nzd.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Colors are pretty, but what do they mean? Let’s add a legend.</p>
<pre>
us_states@bbox #Get us_states bounding box coordinates.
</pre>
<pre> ##min max
 ## r1 -124.73142 -66.96985
 ## r2 24.95597 49.37173</pre>
<pre>
plot(us_states)
choropleth(vulgarisSpatial, vulgarisSpatial$Elev,shading = shades, add = T, pch = 20)
par(xpd=TRUE) #Allow plotting outside of plot area.
choro.legend(-124, 30, shades, cex = .75, title = &amp;quot;Elevation in Meters&amp;quot;) # Plot legend in bottom left. Takes standard legend() params.
</pre>
<p><img title="Plot3" alt="alt text" src="https://i2.wp.com/thepracticaldev.s3.amazonaws.com/i/1g2o2k4xei3rwkxjnpfc.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>It looks like there’s a lot going on in the Northeastern states. For a<br>
closer look, create another clipping (like above) and plot it. Using the<br>
structure below, we can create a selection vector. I have hidden the<br>
full code since it is repetitive (check <a target="_blank" rel="nofollow" href="https://github.com/kiefersmith/WordpressMD">GitHub</a> for the full code.)</p>
<pre>
index '...'
</pre>
<pre>
plot(us_states[index,])
choropleth(vulgarisNE, vulgarisNE$Elev,shading = shades, add = T, pch = 20)
par(xpd = T)
choro.legend(-73, 39.75, shades, cex = .75, title = &amp;quot;Elevation in Meters&amp;quot;)
</pre>
<p><img title="Plot4" alt="alt text" src="https://i0.wp.com/thepracticaldev.s3.amazonaws.com/i/fdaheorcg0jr8zq0oepb.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Hopefully this has been a useful introduction (or refresher) on spatial<br>
data. I always learn a lot in the process of writing these posts. If you<br>
have any ideas or suggestions please leave a comment or feel free to<br>
<a target="_blank" rel="nofollow" href="mailto:kieferisgreat@gmail.com">contact me</a>!</p>
<p>Happy mapping,</p>
<p>Kiefer</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/realdataweb.wordpress.com/516/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/realdataweb.wordpress.com/516/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://realdataweb.wordpress.com/2017/04/22/using-r-as-a-gis/"> R – Real Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/A2v7ZuQpydo"></a></div>