Building a choropleth map of Italy using mapIT(转)

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 [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:

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

时间: 2025-01-05 10:36:05

Building a choropleth map of Italy using mapIT(转)的相关文章

map和vector的迭代器失效问题

当删除一个STL容器(比如map, vector)中的某个元素时, 会引起迭代器失效, 所以, 我们务必提高警惕. 题目: 删除map<int, int>中value为5的倍数的元素. 该题看起来很自然很简单, 实则有迭代器失效的陷阱. 如果对迭代器失效问题一无所知, 则很容易写出如下的错误代码: 1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 typedef map<int, i

info AI drive

Who we look for Here at comma, we don't care about the source of your education or your traditional puffed up resume, we care about your abilities. We are looking for the following traits: Competitors People who have done well at math competitions(US

C++基础之容器

顺序容器简介: 顺序容器类型 描述 vector 可变大小数组,支持快速访问,在尾部之外的地方插入或删除时可能很慢 deque 双端队列.支持快速访问,在头尾插入删除会很快. list 双向列表.只支持双向顺序访问.插入删除很快 forward_list 单向列表.只支持单向顺序访问,在任何位置插入或删除都很快 array 固定大小数组,支持快速随机访问,不能添加删除元素 string 与vector类似,专门用于保存字符.随机访问快,在尾部添加删除快 其中array和forward_list是

USER STORIES AND USE CASES - DON’T USE BOTH

We’re in Orlando for a working session as part of the Core Team building BABOK V3 and over dinner this evening we got to discussing the relationship between user stories and use cases (it wasn't the only thing we discussed, we are also a social group

解析Ceph: 数据的端到端正确性和 Scrub 机制

转自:https://www.ustack.com/blog/ceph-internal-scrub/ Ceph 的主要一大特点是强一致性,这里主要指端到端的一致性.众所周知,传统存储路径上从应用层到内核的文件系统.通用块层.SCSI层到最后的HBA和磁盘控制器,每层都有发生错误的可能性,因此传统的端到端解决方案会以数据块校验为主来解决(这方面的讨论可以参考SCSI中端到端校验能解决数据完整性问题吗).而在 Ceph 方面,更是加入了 Ceph 自己的客户端和网络.存储逻辑.数据迁移,势必导致更

Dapper完美兼容Oracle,执行存储过程,并返回结果集。

这个问题,困扰了我整整两天. 刚刚用到Dapper的时候,感觉非常牛掰.特别是配合.net 4.0新特性dynamic,让我生成泛型集合,再转json一气呵成. 不过,各种ORM总有让人吐槽的地方... 比如,我之前在SqlServer上写测试,搞封装,没有任何问题.CURD.批量操作.存储过程.事物等. 可是以转到Oracle上,就出问题了[喂~不是说好的支持Oracle的么] 在写Dapper+Oracle单元测试的前期,是没有问题的,也就是说普通的Sql操作是没有任何问题的. 然后,我写到

Ceph源码解析:Scrub故障检测

转载请注明出处 陈小跑 http://www.cnblogs.com/chenxianpao/p/5878159.html 本文只梳理了大致流程,细节部分还没搞的太懂,有时间再看,再补充,有错误请指正,谢谢. Ceph 的主要一大特点是强一致性,这里主要指端到端的一致性.众所周知,传统存储路径上从应用层到内核的文件系统.通用块层.SCSI层到最后的HBA和磁盘控制器,每层都有发生错误的可能性,因此传统的端到端解决方案会以数据块校验为主来解决.而在 Ceph 方面,更是加入了 Ceph 自己的客户

LeetCode—Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 其实就是找到序列中出现次数最多的元素,而且已经提出假设这

GeoSpark入门-可视化

????GeoSpark是一种用于大规模空间数据处理的集群计算. GeoSpark通过一组out-of-the-box空间弹性分布式数据集( SRDDs ) 扩展 Apache Spark,它可以跨机器高效地加载.处理.分析.展示大规模空间数据. 准备工作 Windows 和 spark IDEA GeoSpark支持Java.Scala两种,本次开发语言选择Java. GeoSpark 参考https://github.com/jiayuasu/GeoSparkTemplateProject,