Creating Dom. Rep. Static Maps in R

The Dominican Republic Energy Generation Distribution

John Hernandez Valerio
6 min readMar 7, 2021

Table of content

  1. Preparation
  2. Importing and Manipulating data
  3. Creating Energy Sources Distributions Maps
  4. References

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.
At the end we will have something like the following images:

Some of the final map creations

1. Preparation

I am using Rstudio. If haven't installed it yet please refer to the Rstudio download page.

To start getting our hands dirty let’s install and load the following packages:

  • tmap -> To graph spatial polygons.
  • rgdal -> To read “spatialpolygonsdataframe” files.
  • tidyverse -> To visualize and modify data.
  • readxl -> To read __Excel__ Files
  • leaflet -> For interactive maps
  • raster -> Mainly to download __Shape__ files.
  • gridExtra -> In case you want to create a grid of graphs.
  • sf -> To manage Spatial type data.
  • Viridis -> Maps.
x <- c("ggmap", "rgdal", "rgeos", "maptools", "dplyr", "tidyr", "tmap", "ggplot2","leaflet","raster","gridExtra", "sf","viridis","readxl")#install.packages(x)
lapply(x,library, character.only = TRUE)

Note: I have started the line where I install the packages with a # changing it to comment because I don't want to install the packages again every time I load them. If you haven’t installed the packages remember to delete the # symbol.

2. Importing and Manipulating our data

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 link.
By now you have downloaded a bunch of ShapeFiles. Extract the documents and save them (all of them at once) in your project folder.

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

Having the .shp National Cartography’s files, we can import the files corresponding to regions and/or provinces using the readOGR() function and save them into variables (I have called these variables map_region and map_province, you can have your own name).

```{r, warning=FALSE}
map_region <- readOGR(dsn="J:/YOUR ROUTE/REGCenso2010.shp")
map_province <-readOGR(dsn = "J:/YOUR ROUTE/PROVCenso2010.shp")
```

The dsn 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 \ symbol in the route for /.

Calling str(map_region) and str(map_province) we can observe that the SpatialPolygonsDataFrame, or map_region$data and map_province$data, 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.

Result of str(map_region)
Summary code of the variable map_region.

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't provide the level of detail I'm interested in.

Dominican Republic Map divided by Provinces.

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 client statistics 2015.

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 head() code does).

The header of the Energy Sources distribution table.
Merging created table with the map_province

Everything ready for creating our maps.

3. Creating Statics Energy Sources Distribution Maps

Let'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.

```{r, warning=FALSE}
#tmap_options_reset()
tmap_mode("plot")+
tm_shape(map_province2) + tm_borders(col="navajowhite4")+
tm_bubbles(size = "Hydroelectric", col = "blue") + tm_tiles("Hydroelectric (MW)")+
tm_bubbles(size = "Hydroelectric", col = "blue") + tm_tiles("Hydroelectric(MW)")+
tm_bubbles(size = "Natural_Gas", col = "mediumvioletred") +tm_tiles("Natural Gas(MW)")+
tm_bubbles(size = "Solar", col = "gold") + tm_tiles("Solar (MW)")+
tm_bubbles(size = "Wind", col = "chartreuse") + tm_tiles("Wind (MW)")+
tm_bubbles(size = "Coal", col = "black") + tm_tiles("Coal (MW)")+
tm_bubbles(size = "Diesel", col = "chocolate") +tm_tiles("Diesel (MW)")+
tm_bubbles(size = "Fuel_Oil", col = "brown") + tm_tiles("Fuel_Oil (MW)")+
tm_scale_bar()+ tm_compass(type= "8star", position = c("right","top"))+
tm_layout(legend.outside = TRUE,scale = 0.9)
```

Since I want to create a static map we need to set the mode to plot using tmap_mode(“plot”).
Adding tm_shape(map_province2) + tm_borders(col=”navajowhite4") permit use to divide the map by Provinces and change its border color.
The tm_bubbles(size = “Hydroelectric”, col = “blue”) create the first circles with the size of the value of the Hydroelectric variable and the blue color. You can repeat the same for the other variables as shown in the code.

A complete option of colors for the bubbles can be found here.

The tm_compass(type= “8star”, position = c(“right”,”top”)) part of the code include the compass and its position.
The tm_scale_bar() add the scale bar at the bottom.
The tm_layout(legend.outside = TRUE, scale = 0.9) makes the legend go outside the map area since leaving it inside would look messy.

Now let's create maps by each of the Energy Sources and see where is the concentration of every source.

The difference in the following code compare to the previous one is the tm_shape(map_province2)+tm_polygons(col=”Hydroelectric”, palette= “Blues”, title = “Hydroelectric (MW)”) line, this helps us to concentrate on the variable fuel_oil in every Province and give it a color intensity according to its value.

The palette combination of colors can be found here.

Hydroeletric<-tm_shape(map_province2)+tm_polygons(col="Hydroelectric", palette= "Blues", title = "Hydroelectric (MW)")+
tm_scale_bar()+ tm_compass(type= "8star",position = c("right","top"))
Hydroeletric
Hydroelectric Generation distribution in the Dom. Rep.

To include the names of the provinces in each division we just need to add…

tm_text("TOPONIMIA", size = 0.45, fontfamily = "sans")

The result would look like this:

Solar Energy distribution in the Dom. Rep.

The code for the others is lovely repetition so we will no have it here. However, the following figures present the missing Energy sources.

Energy Sources Distribution in the Dom. Rep. territory

If you have any questions and/or suggestions feel free to let me know in the comments or by email.

4. References

Geocomputation with R by Robin Lovelace, Jakub Nowosad, Jannes Muenchow

5 pasos para crear mapas de republica by Nerys Ramierz.

T_map: Get started by Cran-r

Creando mapas de republica dominicana en r by Johan Rosa.

Shapefiles by ArcGIS

--

--

John Hernandez Valerio

M.S. in Applied Mathematics| Data Analyst | Mathematics and Comp. Science Professor