<ahref="/05-mapping_with_r.html"class="sidebar-item-text sidebar-link"><spanclass='chapter-number'>5</span><spanclass='chapter-title'>Mapping With R</span></a>
</div>
</li>
<liclass="sidebar-item">
<divclass="sidebar-item-container">
<ahref="/07-basic_statistics.html"class="sidebar-item-text sidebar-link"><spanclass='chapter-number'>6</span><spanclass='chapter-title'>Basic statistics for spatial analysis</span></a>
<li><ahref="#spatial-in-r-history-and-evolutions"id="toc-spatial-in-r-history-and-evolutions"><spanclass="toc-section-number">1.2</span><spanclass="header-section-number">1.2</span> Spatial in R : History and evolutions</a></li>
<li><ahref="#the-package-sf"id="toc-the-package-sf"><spanclass="toc-section-number">1.3</span><spanclass="header-section-number">1.3</span> The package <code>sf</code></a>
<ul>
<li><ahref="#format-of-spatial-objects-sf"id="toc-format-of-spatial-objects-sf"><spanclass="toc-section-number">1.3.1</span><spanclass="header-section-number">1.3.1</span> Format of spatial objects <code>sf</code></a></li>
<li><ahref="#the-package-terra"id="toc-the-package-terra"><spanclass="toc-section-number">1.5</span><spanclass="header-section-number">1.5</span> The package <code>terra</code></a></li>
<p>The installation part is based on “<ahref="https://intro2r.com/">An Introduction to R</a>” book writed by <em>Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto & David Lusseau</em></p>
<h5data-number="1.1.1.1.1"><spanclass="header-section-number">1.1.1.1.1</span> Windows users</h5>
<p>For Windows users select the ‘<ahref="(https://cran.r-project.org/bin/windows/)">Download R for Windows</a>’ link and then click on the ‘base’ link and finally the download link ‘Download R 4.2.1 for Windows’. This will begin the download of the ‘.exe’ installation file. When the download has completed double click on the R executable file and follow the on-screen instructions. Full installation instructions can be found at the <ahref="https://cran.r-project.org/bin/windows/">CRAN website</a>.</p>
<h5data-number="1.1.1.1.2"><spanclass="header-section-number">1.1.1.1.2</span> Mac users</h5>
<p>For Mac users select the ‘<ahref="https://cran.r-project.org/bin/macosx/">Download R for (Mac) OS X</a>’ link. The binary can be downloaded by selecting the ‘R-4.2.1.pkg’. Once downloaded, double click on the file icon and follow the on-screen instructions to guide you through the necessary steps. See the ‘<ahref="https://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html">R for Mac OS X FAQ</a>’ for further information on installation.</p>
<h5data-number="1.1.1.1.3"><spanclass="header-section-number">1.1.1.1.3</span> Linux users</h5>
<p>For Linux users, the installation method will depend on which flavour of Linux you are using. There are reasonably comprehensive instruction <ahref="https://cran.r-project.org/bin/linux/">here</a> for Debian, Redhat, Suse and Ubuntu. In most cases you can just use your OS package manager to install R from the official repository. On Ubuntu fire up a shell (Terminal) and use (you will need root permission to do this):</p>
<p>which will install base R and also the development version of base R (you only need this if you want to compile R packages from source but it doesn’t hurt to have it).</p>
<p>If you receive an error after running the code above you may need to add a ‘source.list’ entry to your etc/apt/sources.list file. To do this open the terminal and enter this:</p>
<p>Whilst its eminently possible to just use the base installation of R (many people do), we will be using a popular Integrated Development Environment (IDE) called RStudio. RStudio can be thought of as an add-on to R which provides a more user-friendly interface, incorporating the R Console, a script editor and other useful functionality (like R markdown and Git Hub integration). You can find more information about RStudio <ahref="https://rstudio.com/">here</a>.</p>
<p>RStudio is freely available for Windows, Mac and Linux operating systems and can be downloaded from the <ahref="https://rstudio.com/products/rstudio/download">RStudio site</a>. You should select the ‘RStudio Desktop’ version. Note: you must install R before you install RStudio.</p>
<h5data-number="1.1.1.2.1"><spanclass="header-section-number">1.1.1.2.1</span> Windows and Mac users</h5>
<p>For Windows and Mac users you should be presented with the appropriate link for downloading. Click on this link and once downloaded run the installer and follow the instructions. If you don’t see the link then scroll down to the ‘All Installers’ section and choose the link manually.</p>
<h5data-number="1.1.1.2.2"><spanclass="header-section-number">1.1.1.2.2</span> Linux users</h5>
<p>For Linux users scroll down to the ‘All Installers’ section and choose the appropriate link to download the binary for your Linux operating system. RStudio for Ubuntu (and Debian) is available as a <code>*.deb</code> package.</p>
<p>To install the <code>*.deb</code> file navigate to where you downloaded the file and then enter the following command with root permission</p>
<p>The R help is very useful for the use of functions.</p>
<divclass="cell">
<divclass="sourceCode"id="cb6"><preclass="sourceCode r cell-code"><codeclass="sourceCode r"><spanid="cb6-1"><ahref="#cb6-1"aria-hidden="true"tabindex="-1"></a>?plot <spanclass="co">#displays the help page for the plot function</span></span>
<p>Calling the help opens a page (the exact behavior depends on the operating system) with information and usage examples about the documented function(s) or operators.</p>
<h2data-number="1.2"><spanclass="header-section-number">1.2</span> Spatial in R : History and evolutions</h2>
<p>Historically, 4 packages make it possible to import, manipulate and transform spatial data:</p>
<ul>
<li>The package <code>rgdal</code><spanclass="citation"data-cites="rgdal">(<ahref="#ref-rgdal"role="doc-biblioref">Bivand, Keitt, and Rowlingson 2022</a>)</span> which is an interface between R and the <ahref="http://www.gdal.org/">GDAL</a><spanclass="citation"data-cites="GDAL">(<ahref="#ref-GDAL"role="doc-biblioref">GDAL/OGR contributors, n.d.</a>)</span> and <ahref="https://proj.org/">PROJ</a><spanclass="citation"data-cites="PROJ">(<ahref="#ref-PROJ"role="doc-biblioref">PROJ contributors 2021</a>)</span> libraries allow you to import and export spatial data (shapefiles for example) and also to manage cartographic projections<br/>
</li>
<li>The package <code>sp</code><spanclass="citation"data-cites="sp">(<ahref="#ref-sp"role="doc-biblioref">E. J. Pebesma and Bivand 2005</a>)</span> provides class and methods for vector spatial data in R. It allows displaying background maps, inspectiong an attribute table etc.<br/>
</li>
<li>The package <code>rgeos</code><spanclass="citation"data-cites="rgeos">(<ahref="#ref-rgeos"role="doc-biblioref">Bivand and Rundel 2021</a>)</span> gives access to the <ahref="http://trac.osgeo.org/geos/">GEOS</a> spatial operations library and therefore makes classic GIS operations available: calculation of surfaces or perimeters, calculation of distances, spatial aggregations, buffer zones, intersections, etc.<br/>
</li>
<li>The package <code>raster</code><spanclass="citation"data-cites="raster">(<ahref="#ref-raster"role="doc-biblioref">Hijmans 2022a</a>)</span> is dedicated to the import, manipulation and modeling of raster data.</li>
</ul>
<p>Today, the main developments concerning vector data have moved away from the old 3 (<code>sp</code>, <code>rgdal</code>, <code>rgeos</code>) to rely mainly on the package <code>sf</code> (<spanclass="citation"data-cites="sf">(<ahref="#ref-sf"role="doc-biblioref">E. Pebesma 2018a</a>)</span>, <spanclass="citation"data-cites="pebesma2018">(<ahref="#ref-pebesma2018"role="doc-biblioref">E. Pebesma 2018b</a>)</span>). In this manual we will rely exclusively on this package to manipulate vector data.</p>
<p>The packages <code>stars</code><spanclass="citation"data-cites="stars">(<ahref="#ref-stars"role="doc-biblioref">E. Pebesma 2021</a>)</span> and <code>terra</code><spanclass="citation"data-cites="terra">(<ahref="#ref-terra"role="doc-biblioref">Hijmans 2022b</a>)</span> come to replace the package <code>raster</code> for processing raster data. We have chosen to use the package here <code>terra</code> for its proximity to the <code>raster</code>.</p>
<h2data-number="1.3"><spanclass="header-section-number">1.3</span> The package <code>sf</code></h2>
<p><imgsrc="img/sf.gif"align="right"width="150"/> The package <code>sf</code> was released in late 2016 by Edzer Pebesma (also author of <code>sp</code>). Its goal is to combine the feature of <code>sp</code>, <code>rgeos</code> and <code>rgdal</code> in a single, more ergonomic package. This package offers simple objects (following the <ahref="https://en.wikipedia.org/wiki/Simple_Features"><em>simple feature</em></a> standard) which are easier to manipulate. Particular attention has been paid to the compatibility of the package with the <em>pipe</em> syntax and the operators of the <code>tidyverse</code>.</p>
<p><code>sf</code> directly uses the GDAL, GEOS and PROJ libraries.</p>
<p>Website of package <code>sf</code> : <ahref="https://r-spatial.github.io/sf/">Simple Features for R</a></p>
</div>
</div>
</div>
<p>Many of the spatial data available on the internet are in shapefile format, which can be opened in the following way</p>
<divclass="cell">
<divclass="sourceCode"id="cb9"><preclass="sourceCode r cell-code"><codeclass="sourceCode r"><spanid="cb9-1"><ahref="#cb9-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">library</span>(sf)</span></code></pre></div>
<divclass="cell-output cell-output-stderr">
<pre><code>Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE</code></pre>
</div>
<divclass="sourceCode"id="cb11"><preclass="sourceCode r cell-code"><codeclass="sourceCode r"><spanid="cb11-1"><ahref="#cb11-1"aria-hidden="true"tabindex="-1"></a>district <spanclass="ot"><-</span><spanclass="fu">st_read</span>(<spanclass="st">"data_cambodia/district.shp"</span>)</span></code></pre></div>
<divclass="cell-output cell-output-stdout">
<preclass="code-out"><code>Reading layer `district' from data source
<p>For the multiple limitations of this format (multi-file, limited number of records…) we advise you to prefer another format such as the geopackage <code>*.gpkg</code>. All the good reasons not to use the shapefile are <ahref="http://switchfromshapefile.org/">here</a>.</p>
</div>
</div>
<p>A geopackage is a database, to load a layer, you must know its name</p>
<divclass="cell">
<divclass="sourceCode"id="cb13"><preclass="sourceCode r cell-code"><codeclass="sourceCode r"><spanid="cb13-1"><ahref="#cb13-1"aria-hidden="true"tabindex="-1"></a><spanclass="fu">st_layers</span>(<spanclass="st">"data_cambodia/cambodia.gpkg"</span>)</span></code></pre></div>
<divclass="cell-output cell-output-stdout">
<preclass="code-out"><code>Driver: GPKG
Available layers:
layer_name geometry_type features fields crs_name
1 country Multi Polygon 1 10 WGS 84 / UTM zone 48N
2 district Multi Polygon 197 10 WGS 84 / UTM zone 48N
3 education Multi Polygon 25 19 WGS 84 / UTM zone 48N
4 hospital Point 956 13 WGS 84 / UTM zone 48N
5 cases Multi Point 972 2 WGS 84 / UTM zone 48N
6 road Multi Line String 6 9 WGS 84 / UTM zone 48N</code></pre>
</div>
</div>
<divclass="cell">
<divclass="sourceCode"id="cb15"><preclass="sourceCode r cell-code"><codeclass="sourceCode r"><spanid="cb15-1"><ahref="#cb15-1"aria-hidden="true"tabindex="-1"></a>road <spanclass="ot"><-</span><spanclass="fu">st_read</span>(<spanclass="st">"data_cambodia/cambodia.gpkg"</span>, <spanclass="at">layer =</span><spanclass="st">"road"</span>)</span></code></pre></div>
<divclass="cell-output cell-output-stdout">
<preclass="code-out"><code>Reading layer `road' from data source
<p>Objects<code>sf</code> are objects in <code>data.frame</code> which one of the columns contains geometries. This column is the class of sfc (<em>simple feature column</em>) and each individual of the column is a sfg <em>(simple feature geometry)</em>. This format is very practical insofa as the data and the geometries are intrinsically linked in the same object.</p>
<p>The free R software spatial ecosystem is rich, dynamic and mature and several packages allow to import, process and represent spatial data. The package <ahref="https://CRAN.R-project.org/package=maps"><code>mapsf</code></a><spanclass="citation"data-cites="mapsf">(<ahref="#ref-mapsf"role="doc-biblioref">Giraud 2022</a>)</span> relies on this ecosystem to integrate the creation of quality thematic maps into processing chains with R.</p>
<p>Other packages can be used to make thematic maps. The package <code>ggplot2</code><spanclass="citation"data-cites="ggplot2">(<ahref="#ref-ggplot2"role="doc-biblioref">Wickham 2016</a>)</span>, in association with the package <code>ggspatial</code><spanclass="citation"data-cites="ggspatial">(<ahref="#ref-ggspatial"role="doc-biblioref">Dunnington 2021</a>)</span>, allows for example to display spatial objects and to make simple thematic maps. The package <code>tmap</code><spanclass="citation"data-cites="tmap">(<ahref="#ref-tmap"role="doc-biblioref">Tennekes 2018</a>)</span> is dedicated to the creation of thematic maps, it uses a syntax close to that of <code>ggplot2</code> (sequence of instructions combined with the ‘+’ sign). Documentation and tutorials for using these two packages are readily available on the web.</p>
<p>Here, we will mainly use the package <code>mapsf</code> whose functionalities are quite complete and the handling rather simple. In addition, the package is relatively light.</p>
<p><code>mapsf</code> allows you to create most of the types of map usually used in statistical cartography (choropleth maps, typologies, proportional or graduated symbols, etc.). For each type of map, several parameters are used to customize the cartographic representation. These parameters are the same as those found in the usual GIS or cartography software (for example, the choice of discretizations and color palettes, the modification of the size of the symbols or the customization of the legends). Associated with the data representation functions, other functions are dedicated to cartographic dressing (themes or graphic charters, legends, scales, orientation arrows, title, credits, annotations, etc.), the creation of boxes or the exporting maps.<br/>
<code>mapsf</code> is the successor of <ahref="http://riatelab.github.io/cartography/docs/"><code>cartography</code></a><spanclass="citation"data-cites="cartography">(<ahref="#ref-cartography"role="doc-biblioref">Giraud and Lambert 2016</a>)</span>, it offers the same main functionalities while being lighter and more ergonomic.</p>
<p>To use this package several sources can be consulted:</p>
<ul>
<li><p>The package documentation accessible <ahref="http://riatelab.github.io/mapsf/">on the internet</a> or directly in R (<code>?mapsf</code>),</p></li>
<li><p>The <ahref="https://riatelab.github.io/mapsf/articles/">vignettes</a> associated with the package show sample scripts,</p></li>
<li><p>The <ahref="https://rgeomatic.hypotheses.org/">R Geomatics</a> blog which provides resources and examples related to the package and more generally to the R spatial ecosystem.</p></li>
<h2data-number="1.5"><spanclass="header-section-number">1.5</span> The package <code>terra</code></h2>
<p><imgsrc="img/logo_terra.png"align="right"width="150"/> The package <code>terra</code> was release in early 2020 by Robert J. Hijmans (also author of <code>raster</code>). Its objective is to propose methods of treatment and analysis of raster data. This package is very similar to the package <code>raster</code>; but it has more features, it’s easier to use, and it’s faster.</p>
<p><spanclass="hidden"data-render-id="quarto-int-sidebar-title">Mapping and spatial analyses in R for One Health studies</span><spanclass="hidden"data-render-id="quarto-int-navbar-title">Mapping and spatial analyses in R for One Health studies</span><spanclass="hidden"data-render-id="quarto-int-next"><spanclass="chapter-number">2</span><spanclass="chapter-title">Data Acquisition</span></span><spanclass="hidden"data-render-id="quarto-int-prev">Preface</span><spanclass="hidden"data-render-id="quarto-int-sidebar:/index.html">Preface</span><spanclass="hidden"data-render-id="quarto-int-sidebar:/01-introduction.html"><spanclass="chapter-number">1</span><spanclass="chapter-title">Introduction</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/02-data_acquisition.html"><spanclass="chapter-number">2</span><spanclass="chapter-title">Data Acquisition</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/03-vector_data.html"><spanclass="chapter-number">3</span><spanclass="chapter-title">Using vector data</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/04-raster_data.html"><spanclass="chapter-number">4</span><spanclass="chapter-title">Using raster data</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/05-mapping_with_r.html"><spanclass="chapter-number">5</span><spanclass="chapter-title">Mapping With R</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/07-basic_statistics.html"><spanclass="chapter-number">6</span><spanclass="chapter-title">Basic statistics for spatial analysis</span></span><spanclass="hidden"data-render-id="quarto-int-sidebar:/08-exercices.html">Exercice</span><spanclass="hidden"data-render-id="quarto-int-sidebar:/references.html">References</span><spanclass="hidden"data-render-id="footer-left">UMR 228 ESPACE-DEV</span><spanclass="hidden"data-render-id="footer-right"><imgsrc="img/ird_footer.png"height="50"/></span></p>
</div>
<divid="quarto-meta-markdown"class="hidden">
<p><spanclass="hidden"data-render-id="quarto-metatitle">Mapping and spatial analyses in R for One Health studies - <spanid="introductionx"class="quarto-section-identifier"><spanclass="chapter-number">1</span><spanclass="chapter-title">Introduction</span></span></span><spanclass="hidden"data-render-id="quarto-twittercardtitle">Mapping and spatial analyses in R for One Health studies - <spanid="introductionx"class="quarto-section-identifier"><spanclass="chapter-number">1</span><spanclass="chapter-title">Introduction</span></span></span><spanclass="hidden"data-render-id="quarto-ogcardtitle">Mapping and spatial analyses in R for One Health studies - <spanid="introductionx"class="quarto-section-identifier"><spanclass="chapter-number">1</span><spanclass="chapter-title">Introduction</span></span></span><spanclass="hidden"data-render-id="quarto-metasitename">Mapping and spatial analyses in R for One Health studies</span></p>
Bivand, Roger, Tim Keitt, and Barry Rowlingson. 2022. <span>“Rgdal: Bindings for the ’Geospatial’ Data Abstraction Library.”</span><ahref="https://CRAN.R-project.org/package=rgdal">https://CRAN.R-project.org/package=rgdal</a>.
Bivand, Roger, and Colin Rundel. 2021. <span>“Rgeos: Interface to Geometry Engine - Open Source (’GEOS’).”</span><ahref="https://CRAN.R-project.org/package=rgeos">https://CRAN.R-project.org/package=rgeos</a>.
Dunnington, Dewey. 2021. <span>“Ggspatial: Spatial Data Framework for Ggplot2.”</span><ahref="https://CRAN.R-project.org/package=ggspatial">https://CRAN.R-project.org/package=ggspatial</a>.
Giraud, Timothée, and Nicolas Lambert. 2016. <span>“Cartography: Create and Integrate Maps in Your r Workflow”</span> 1. <ahref="https://doi.org/10.21105/joss.00054">https://doi.org/10.21105/joss.00054</a>.
Hijmans, Robert J. 2022a. <span>“Raster: Geographic Data Analysis and Modeling.”</span><ahref="https://CRAN.R-project.org/package=raster">https://CRAN.R-project.org/package=raster</a>.
Pebesma, Edzer. 2018a. <span>“<span></span>Simple Features for r: Standardized Support for Spatial Vector Data<span></span>”</span> 10. <ahref="https://doi.org/10.32614/RJ-2018-009">https://doi.org/10.32614/RJ-2018-009</a>.
———. 2018b. <span>“Simple Features for R: Standardized Support for Spatial Vector Data.”</span><em>The R Journal</em> 10 (1): 439. <ahref="https://doi.org/10.32614/rj-2018-009">https://doi.org/10.32614/rj-2018-009</a>.
———. 2021. <span>“Stars: Spatiotemporal Arrays, Raster and Vector Data Cubes.”</span><ahref="https://CRAN.R-project.org/package=stars">https://CRAN.R-project.org/package=stars</a>.
Pebesma, Edzer J., and Roger S. Bivand. 2005. <span>“Classes and Methods for Spatial Data in <span></span>r<span></span>”</span> 5. <ahref="https://CRAN.R-project.org/doc/Rnews/">https://CRAN.R-project.org/doc/Rnews/</a>.
Wickham, Hadley. 2016. <span>“Ggplot2: Elegant Graphics for Data Analysis.”</span><ahref="https://ggplot2.tidyverse.org">https://ggplot2.tidyverse.org</a>.
</div>
</div>
</section>
</main><!-- /main -->
<script id = "quarto-html-after-body"type="application/javascript">