<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by John Hernandez Valerio on Medium]]></title>
        <description><![CDATA[Stories by John Hernandez Valerio on Medium]]></description>
        <link>https://medium.com/@johnhernandezv?source=rss-ee21905669cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pbstwc0lqS51ZYvxoi537w.jpeg</url>
            <title>Stories by John Hernandez Valerio on Medium</title>
            <link>https://medium.com/@johnhernandezv?source=rss-ee21905669cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:06:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@johnhernandezv/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Dom. Rep. Static Maps in R]]></title>
            <link>https://johnhernandezv.medium.com/building-dom-rep-static-maps-in-r-d3f34b6259b1?source=rss-ee21905669cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3f34b6259b1</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[maps]]></category>
            <category><![CDATA[renewable-energy-sources]]></category>
            <category><![CDATA[rstudio]]></category>
            <category><![CDATA[dominican-republic]]></category>
            <dc:creator><![CDATA[John Hernandez Valerio]]></dc:creator>
            <pubDate>Sun, 07 Mar 2021 07:05:44 GMT</pubDate>
            <atom:updated>2021-03-07T07:53:14.751Z</atom:updated>
            <content:encoded><![CDATA[<h3>Creating Dom. Rep. Static Maps in R</h3><h4>The Dominican Republic Energy Generation Distribution</h4><h4>Table of content</h4><ol><li><strong>Preparation</strong></li><li><strong>Importing and Manipulating data</strong></li><li><strong>Creating Energy Sources Distributions Maps</strong></li><li><strong>References</strong></li></ol><p>The purpose of this article is to give you a hand creating Static Maps in R, using for example the Energy Source distribution in The Dominican Republic territory. Also, it would give you a big idea of how is the energy generation distribute in the country.<br>At the end we will have something like the following images:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*if7wN9TsYRsnnKKr7DgFBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2bJhU901oZzcNZY14sXNFQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pgG5F5Oz2Q25saxpvUN1-Q.png" /><figcaption>Some of the final map creations</figcaption></figure><h4>1. Preparation</h4><p>I am using <a href="https://en.wikipedia.org/wiki/RStudio">Rstudio</a>. If haven&#39;t installed it yet please refer to the Rstudio download page.</p><p><a href="https://rstudio.com/products/rstudio/download/">Download the RStudio IDE</a></p><p>To start getting our hands dirty let’s install and load the following packages:</p><ul><li>tmap -&gt; To graph spatial polygons.</li><li>rgdal -&gt; To read “spatialpolygonsdataframe” files.</li><li>tidyverse -&gt; To visualize and modify data.</li><li>readxl -&gt; To read __Excel__ Files</li><li>leaflet -&gt; For interactive maps</li><li>raster -&gt; Mainly to download __Shape__ files.</li><li>gridExtra -&gt; In case you want to create a grid of graphs.</li><li>sf -&gt; To manage Spatial type data.</li><li>Viridis -&gt; Maps.</li></ul><pre>x &lt;- c(&quot;ggmap&quot;, &quot;rgdal&quot;, &quot;rgeos&quot;, &quot;maptools&quot;, &quot;dplyr&quot;, &quot;tidyr&quot;, &quot;tmap&quot;, &quot;ggplot2&quot;,&quot;leaflet&quot;,&quot;raster&quot;,&quot;gridExtra&quot;, &quot;sf&quot;,&quot;viridis&quot;,&quot;readxl&quot;)</pre><pre>#install.packages(x)<br>lapply(x,library, character.only = TRUE)</pre><p><strong>Note:</strong> I have started the line where I install the packages with a <em>#</em> changing it to <strong>comment </strong>because I don&#39;t want to install the packages again every time I load them. If you haven’t installed the packages remember to delete the <em>#</em> symbol.</p><h4>2. Importing and Manipulating our data</h4><p>The first thing we need to do is to download The Dominican Republic cartography files from the Statistics National Office (ONE in Spanish). To do so we will access the following <a href="https://www.one.gob.do/Multimedia/Download?ObjId=3320">link</a>.<br>By now you have downloaded a bunch of <strong>ShapeFiles</strong>. Extract the documents and save them (all of them at once) in your project folder.</p><blockquote>A shapefile is an Esri vector data storage format for storing the location, shape, and attributes of geographic features. It is stored as a set of related files and contains one feature class. — ArcGIS</blockquote><p>Having the <strong>.shp</strong> National Cartography’s files, we can import the files corresponding to regions and/or provinces using the <em>readOGR()</em> function and save them into variables (I have called these variables map_region and map_province, you can have your own name).</p><pre>```{r, warning=FALSE}<br>map_region &lt;- readOGR(dsn=&quot;J:/<strong>YOUR ROUTE</strong>/REGCenso2010.shp&quot;)<br>map_province &lt;-readOGR(dsn = &quot;J:/<strong>YOUR ROUTE</strong>/PROVCenso2010.shp&quot;)<br>```</pre><p>The <em>dsn</em> value is the route to where you saved the files. If you don’t know how to find that route, check the following image. Go where you save your files, click on it top route, and copy it. When you paste it in Rstudio you may need to change the <strong>\</strong> symbol in the route for <strong>/.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LlQCw6GnuI_f3rNMjaqD6Q.jpeg" /></figure><p>Calling <strong><em>str(map_region)</em></strong><em> and </em><strong><em>str(map_province)</em></strong><em> </em>we can observe that the <em>SpatialPolygonsDataFrame, or </em><strong><em>map_region$data</em></strong><em> and </em><strong><em>map_province$data</em></strong><em>, </em>have 10 and 32 observations respectively, this corresponds to the number of Regions and Provinces in The Dom. Rep. Subdivided in 2 variables (in the case of the region) $REG and $TOPONIMIA, which represent the number of the region and the name.</p><figure><img alt="Result of str(map_region)" src="https://cdn-images-1.medium.com/max/1024/1*sn5Pk6fkDOH8snWNGu1USg.png" /><figcaption>Summary code of the variable map_region.</figcaption></figure><p>We are ready to take our first look at the map. The following figure shows the Province distribution of the map. For this article, I will focus on the Province distribution (32 divisions) rather than the Region distribution (10 divisions) since this data doesn&#39;t provide the level of detail I&#39;m interested in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q5ZKqhaG3SbWhhKeaZG1rg.png" /><figcaption>Dominican Republic Map divided by Provinces.</figcaption></figure><p>We want to see this map filled with colors, telling us the energy generation story in The Dominican Republic. For this task, we will need to include the data related to the Energy Source Generation. I have created an Excel table with that information extracted from the National Energy Commission of The Dom. Rep. in its file <a href="http://mapas.cne.gob.do/">client statistics 2015</a>.</p><p>A closer look at that table is presented in the following image. Notice that I just wrote the code to present the first 6 rows on the table (this is what the <em>head() </em>code does).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oCCGBLE3qVaDBvovZsFpnw.png" /><figcaption>The header of the Energy Sources distribution table.</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F520495150%3Fapp_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F520495150&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1077997405_1280.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1280" height="720" frameborder="0" scrolling="no"><a href="https://medium.com/media/d95cf2fd3c9b838ac1eac56bc3505129/href">https://medium.com/media/d95cf2fd3c9b838ac1eac56bc3505129/href</a></iframe><p>Everything ready for creating our maps.</p><h4>3. Creating Statics Energy Sources Distribution Maps</h4><p>Let&#39;s start with the general map. This map will use the white map as the first layout and then will have bubbles in the locations that the Energy Source is located, the size of the bubble varies according to the value of the installed Capacity and the color change according to the Source.</p><pre>```{r, warning=FALSE}<br>#tmap_options_reset()<br>tmap_mode(&quot;plot&quot;)+<br>  tm_shape(map_province2) + tm_borders(col=&quot;navajowhite4&quot;)+<br>  tm_bubbles(size = &quot;Hydroelectric&quot;, col = &quot;blue&quot;) + tm_tiles(&quot;Hydroelectric (MW)&quot;)+<br>  tm_bubbles(size = &quot;Hydroelectric&quot;, col = &quot;blue&quot;) + tm_tiles(&quot;Hydroelectric(MW)&quot;)+<br>  tm_bubbles(size = &quot;Natural_Gas&quot;, col = &quot;mediumvioletred&quot;) +tm_tiles(&quot;Natural Gas(MW)&quot;)+<br>  tm_bubbles(size = &quot;Solar&quot;, col = &quot;gold&quot;) + tm_tiles(&quot;Solar (MW)&quot;)+<br>  tm_bubbles(size = &quot;Wind&quot;, col = &quot;chartreuse&quot;) + tm_tiles(&quot;Wind (MW)&quot;)+<br>  tm_bubbles(size = &quot;Coal&quot;, col = &quot;black&quot;) + tm_tiles(&quot;Coal (MW)&quot;)+<br>  tm_bubbles(size = &quot;Diesel&quot;, col = &quot;chocolate&quot;) +tm_tiles(&quot;Diesel (MW)&quot;)+<br>  tm_bubbles(size = &quot;Fuel_Oil&quot;, col = &quot;brown&quot;) +  tm_tiles(&quot;Fuel_Oil (MW)&quot;)+<br>  tm_scale_bar()+ tm_compass(type= &quot;8star&quot;, position = c(&quot;right&quot;,&quot;top&quot;))+<br>  tm_layout(legend.outside = TRUE,scale = 0.9)</pre><pre>```</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2bJhU901oZzcNZY14sXNFQ.jpeg" /></figure><p>Since I want to create a static map we need to set the mode to plot using <strong>tmap_mode(“plot”). <br></strong>Adding <strong>tm_shape(map_province2) + tm_borders(col=”navajowhite4&quot;) </strong>permit use to divide the map by Provinces and change its border color.<br>The <strong>tm_bubbles(size = “Hydroelectric”, col = “blue”) </strong>create the first circles with the size of the value of the <em>Hydroelectric </em>variable and the blue color. You can repeat the same for the other variables as shown in the code.</p><p>A complete option of colors for the bubbles can be found <a href="http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf?utm_source=twitterfeed&amp;utm_medium=twitter">here</a>.</p><p>The <strong>tm_compass(type= “8star”, position = c(“right”,”top”)) </strong>part of the code include the compass and its position.<br>The <strong>tm_scale_bar() </strong>add the scale bar at the bottom.<br>The <strong>tm_layout(legend.outside = TRUE, scale = 0.9) </strong>makes the legend go outside the map area since leaving it inside would look messy.</p><p>Now let&#39;s create maps by each of the Energy Sources and see where is the concentration of every source.</p><p>The difference in the following code compare to the previous one is the <strong>tm_shape(map_province2)+tm_polygons(col=”Hydroelectric”, palette= “Blues”, title = “Hydroelectric (MW)”) </strong>line, this helps us to concentrate on the variable <em>fuel_oil </em>in every Province and give it a color intensity according to its value.</p><p>The palette combination of colors can be found <a href="https://www.datanovia.com/en/blog/top-r-color-palettes-to-know-for-great-data-visualization/">here</a>.</p><pre>Hydroeletric&lt;-tm_shape(map_province2)+tm_polygons(col=&quot;Hydroelectric&quot;, palette= &quot;Blues&quot;, title = &quot;Hydroelectric (MW)&quot;)+<br>  tm_scale_bar()+ tm_compass(type= &quot;8star&quot;,position = c(&quot;right&quot;,&quot;top&quot;))<br>Hydroeletric</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*68huS110YJ0M2Thcwuja1A.png" /><figcaption>Hydroelectric Generation distribution in the Dom. Rep.</figcaption></figure><p>To include the names of the provinces in each division we just need to add…</p><pre>tm_text(&quot;TOPONIMIA&quot;, size = 0.45, fontfamily = &quot;sans&quot;)</pre><p>The result would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pgG5F5Oz2Q25saxpvUN1-Q.png" /><figcaption>Solar Energy distribution in the Dom. Rep.</figcaption></figure><p>The code for the others is lovely repetition so we will no have it here. However, the following figures present the missing Energy sources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LprXQ0ARhLIpRa8zxvy0Uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rQwx8TgpzxYB1uW8hGcyxA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*if7wN9TsYRsnnKKr7DgFBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ulHWwjZi5GfXF4L2-sOmtw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fMphVOWyle3QLXgle4x8wA.png" /><figcaption>Energy Sources Distribution in the Dom. Rep. territory</figcaption></figure><p>If you have any questions and/or suggestions feel free to let me know in the comments or by <a href="/service/mailto:johnhernandez@outlook.com">email.</a></p><h4>4. References</h4><p><a href="https://geocompr.robinlovelace.net/">Geocomputation with R</a><em> by Robin Lovelace, Jakub Nowosad, Jannes Muenchow</em></p><p><a href="http://betaeconomia.blogspot.com/2017/11/5-pasos-para-crear-mapas-de-republica.html">5 pasos para crear mapas de republica</a> by <em>Nerys Ramierz</em>.</p><p><a href="https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html">T_map: Get started</a> by Cran-r</p><p><a href="https://www.johan-rosa.com/2019/10/23/creando-mapas-de-rep%C3%BAblica-dominicana-en-r/">Creando mapas de republica dominicana en r</a> by Johan Rosa.</p><p><a href="https://doc.arcgis.com/en/arcgis-online/reference/shapefiles.htm">Shapefiles</a> by ArcGIS</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3f34b6259b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>