In the R environment, different packages to draw maps are available. I lost the count by now; surely, sp and ggmap deserve consideration. Despite the great availability of R functions dedicated to this topic, in the past, when I needed to draw a very basic map of Italy with regions marked with different colours (namely a choropleth map), I had a bit of difficulties.
My expectation was that building a choropleth map of Italy using R was a extremely trivial procedure, but my experience was different. In fact, if the aim is to represent a map of United States, the most part of the available functions are very easy to use. However, to draw a map of Italy, the procedures become a bit complicated if compared to the banality of the chart (a good tutorial – in Italian – can be found here).
I wasn’t the only one R user to have this problem. Some time ago, in the community [email protected], Lorenzo di Blasio proposed a good solution using ggplot2. Summarizing the code proposed by Lorenzo, I assembled a first function capable to create a map in a easy and fast way. Finally, Nicola Sturaro of MilanoR group has strongly improved and completed the code and created a new package: mapIT.
Currently, the package mapIT is located into a repository on GitHub. In order to install the package, you can use the package devtools:
In my first use of mapIT, I had to map the number of wineries taken into account in a research regarding Italian wine evaluations. I need to visualize, for each region, the number of wineries whose wines were reviewed. In the following code, there are the data; for each Italian region (first column) the number of wineries (second column) is reported.
wine <- data.frame( Region = c("Abruzzo","Basilicata","Calabria","Campania", "Emilia-Romagna","Friuli-Venezia Giulia","Lazio", "Liguria","Lombardia","Marche","Molise","Piemonte", "Puglia","Sardegna","Sicilia","Toscana", "Trentino-Alto Adige","Umbria","Valle d'Aosta","Veneto"), Wineries = c(22,8,9,35,24,74,19,8,41,29,5,191,22,14,40,173,57,29,6,92) )
The names of regions can be written both in lowercase and in uppercase. Spaces and other non-alphabetical characters will be ignored. So, you can write indifferently: ‘Trentino-Alto Adige’, ‘Trentino Alto Adige’ or ‘TrentinoAltoAdige’. For regions with bilingual denomination, only the Italian wording is accepted.
To build the map, the package mapIT make available the namesake function mapIT(). The first argument to pass to the function is the numeric variable (Wineries) and the second one is the variable specifying the Italian region (Region). A third argument can be used to specify the data frame from which extract the variables.
Further, there are some additional arguments useful to modify the graphic style. In the following example I used guide.label, which specifies the title label for the legend.
library(mapIT) mapIT(Wineries, Region, data=wine, guide.label="Number ofnwineries")
Easy, right? It was enough to load the package and launch a brief row of code!
The chart can be customized in several ways. The main argument allowing to alter the graphic details is graphPar, consisting in a long list of arguments (for details, see the help function).
One of the first things we want to do, surely will be alter the colours. To alter the colours, you must specify, in the graphPar list, the colours for the minimum value (low) and for the maximum value (high):
gp <- list(low="#fff0f0", high="red3")
For convenience I saved the list into the object gp. Note that colours can be specified using both the hexadecimal code and the R keywords for colours.
mapIT(Wineries, Region, data=wine, guide.label="Number ofnwineries", graphPar=gp)
You can play with colours to find your preferred arrangement. To identify the hexadecimal code for colours, a fast solution is to use a web applications as RGB color picker.
The low and high values of graphPar can be used to convert the chart in black and white. In this case, to make the chart a bit more pleasant, it’s possible use the themes of ggplot2. In the examples below, the first map (left panel) was built using the theme theme_bw, while the second map (right panel) was built using the theme theme_grey.
library(ggplot2) # Theme: black and white gp <- list(low="white", high="gray20", theme=theme_bw()) mapIT(Wineries, Region, data=wine, guide.label="Number ofnwineries", graphPar=gp) # Theme: grey gp <- list(low="white", high="gray20", theme=theme_grey()) mapIT(Wineries, Region, data=wine, guide.label="Number ofnwineries", graphPar=gp)
Still there are different features to implement and, in the future, some things can be changed. If you has some ideas to improve mapIT, or you found a malfunctioning, you can open an issue on GitHub.