标签:
In the R environment, different packages to draw maps are available. I lost the count by now; surely, spand 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:
1 library(devtools) 2 install_github("quantide/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.
1 wine <- data.frame( 2 Region = c("Abruzzo","Basilicata","Calabria","Campania", 3 "Emilia-Romagna","Friuli-Venezia Giulia","Lazio", 4 "Liguria","Lombardia","Marche","Molise","Piemonte", 5 "Puglia","Sardegna","Sicilia","Toscana", 6 "Trentino-Alto Adige","Umbria","Valle d\‘Aosta","Veneto"), 7 Wineries = c(22,8,9,35,24,74,19,8,41,29,5,191,22,14,40,173,57,29,6,92) 8 )
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.
1 library(mapIT) 2 mapIT(Wineries, Region, data=wine, guide.label="Number of\nwineries")
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):
1 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.
1 mapIT(Wineries, Region, data=wine, 2 guide.label="Number of\nwineries", 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.
1 library(ggplot2) 2 3 # Theme: black and white 4 gp <- list(low="white", high="gray20", theme=theme_bw()) 5 mapIT(Wineries, Region, data=wine, 6 guide.label="Number of\nwineries", graphPar=gp) 7 8 # Theme: grey 9 gp <- list(low="white", high="gray20", theme=theme_grey()) 10 mapIT(Wineries, Region, data=wine, 11 guide.label="Number of\nwineries", 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.
转自:http://www.milanor.net/blog/?p=1605
Building a choropleth map of Italy using mapIT(转)
标签:
原文地址:http://www.cnblogs.com/payton/p/4238078.html