Building a choropleth map of Italy using mapIT

[This article was first published on MilanoR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 Statistica@Ning, 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:

library(devtools)
install_github("nicolasturaro/mapIT")

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")

mapIT - choropleth map of Italy in blue

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)

mapIT - choropleth map of Italy in red

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)

mapIT - choropleth map of Italy in black and white

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.

To leave a comment for the author, please follow the link and comment on their blog: MilanoR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)