如何使用地图包映射单个美国州(MO)县人口数据?

时间:2017-10-16 19:51:49

标签: r maps

我很难将渐变颜色映射到我使用基本R包地图的某些县级人口数据。我知道颜色必须插入到数据帧中,但我不确定它是如何转换为地图的。以下是我使用的代码:

mysql> describe select * from part4_v2_3colleftjoin;
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+------------------------------------------------+
| id | select_type | table | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                          |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+------------------------------------------------+
|  1 | SIMPLE      | p     | NULL       | index | NULL          | id2  | 4       | NULL |   33 |   100.00 | Using index                                    |
|  1 | SIMPLE      | f     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
|  1 | SIMPLE      | p     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
|  1 | SIMPLE      | f     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
|  1 | SIMPLE      | p     | NULL       | index | id1           | id2  | 4       | NULL |   33 |   100.00 | Using where; Using index                       |
|  1 | SIMPLE      | f     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
|  1 | SIMPLE      | p     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
|  1 | SIMPLE      | f     | NULL       | ALL   | id1,id2       | NULL | NULL    | NULL |   33 |   100.00 | Range checked for each record (index map: 0x3) |
+----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+------------------------------------------------+

如何告诉R以正确的顺序映射颜色?我的感觉告诉我将我的数据附加到地图的内部数据库,但我无法找到正确执行此操作的文档 - 或者,更可能的是,我错了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

要回答您的问题,您需要访问maps包中包含的county.fips数据框。这将有fips编号和州,县名。此列表的映射顺序正确。下面的代码示例提取密苏里州的县并随机为一对夫妇着色以进行验证:

mocounties<-county.fips[grepl('missouri', county.fips$polyname),]
  mocounties$col<-"grey"
  mocounties$col[5]<-"red"
  mocounties$col[15]<-"green"
  mocounties$col[115]<-"blue"

map('county','missouri', interior = T,fill =T,
    col = mocounties$col)