使用ggplot和geom_map绘制正确的颜色

时间:2016-08-24 18:14:32

标签: r ggplot2 maps

我有一个名为regional的数据框:

                       id         Growth    interv  color
1              ALENTEJO CENTRAL   479.11  (475,500] yellow
2              ALENTEJO LITORAL   530.23 (525, Inf] green4
3                       ALGARVE   470.91  (450,475]    red
4                 ALTO ALENTEJO   499.72  (475,500] yellow
5                    ALTO MINHO   519.31  (500,525]  green
6                   ALTO TÂMEGA   460.50  (450,475]    red
7  ÁREA METROPOLITANA DE LISBOA   494.05  (475,500] yellow
8   ÁREA METROPOLITANA DO PORTO   497.15  (475,500] yellow
9                           AVE   484.41  (475,500] yellow
10               BAIXO ALENTEJO   461.79  (450,475]    red
11                  BEIRA BAIXA   511.19  (500,525]  green
12    BEIRAS E SERRA DA ESTRELA   503.52  (500,525]  green
13                       CÁVADO   486.08  (475,500] yellow
14                        DOURO   509.93  (500,525]  green
15              LEZÍRIA DO TEJO   514.67  (500,525]  green
16                   MÉDIO TEJO   504.39  (500,525]  green
17                        OESTE   488.85  (475,500] yellow
18                 R. A. AÇORES   461.74  (450,475]    red
19                R. A. MADEIRA   490.16  (475,500] yellow
20             REGIÃO DE AVEIRO   490.46  (475,500] yellow
21            REGIÃO DE COIMBRA   502.82  (500,525]  green
22             REGIÃO DE LEIRIA   507.60  (500,525]  green
23               TÂMEGA E SOUSA   452.97  (450,475]    red
24     TERRAS DE TRÁS-OS-MONTES   454.03  (450,475]    red
25             VISEU DÃO LAFÕES   514.99  (500,525]  green

我试图使用以下代码绘制最后一列(区域$颜色)中颜色的地图:

niv_leg=c("<450","]450; 475]", "]475;500]","]500; 525]", ">525")
colors = c("red4", "red", "yellow", "green", "green4")

prtnutsiii = ggplot()+
  geom_polygon(data = mapaf, aes(x=long, y = lat, group = group), fill = NA, colour="darkgray", size=0.25)+
  coord_equal()

growth = prtnutsiii +
  geom_map(data = regional, map = mapaf, aes(map_id = id, fill =  color),colour="darkgray", size = 0.25) +
  theme(legend.title = element_blank())
  scale_fill_manual(values = colors,labels = niv_leg)
pisamathg + theme(legend.position = "left")

我得到这张地图: enter image description here

但我面临两个问题,我无法弄清楚:

  1. 我只在图例中获得4个标签(它们应该是5个)
  2. 对于不同的“id”区域,颜色根本没有正确绘制...
  3. mapaf中的“id”与区域数据框中的“id”匹配,因此,我不知道为什么它不起作用...任何帮助将不胜感激。

    P.S。:我使用的shp文件(mapaf)在这里:https://dl.dropboxusercontent.com/u/112525/mapaf.txt

    这是dput(区域性):

    structure(list(id = c("ALENTEJO CENTRAL", "ALENTEJO LITORAL", 
    "ALGARVE", "ALTO ALENTEJO", "ALTO MINHO", "ALTO TÂMEGA", "ÁREA METROPOLITANA DE LISBOA", 
    "ÁREA METROPOLITANA DO PORTO", "AVE", "BAIXO ALENTEJO", "BEIRA BAIXA", 
    "BEIRAS E SERRA DA ESTRELA", "CÁVADO", "DOURO", "LEZÍRIA DO TEJO", 
    "MÉDIO TEJO", "OESTE", "R. A. AÇORES", "R. A. MADEIRA", "REGIÃO DE AVEIRO", 
    "REGIÃO DE COIMBRA", "REGIÃO DE LEIRIA", "TÂMEGA E SOUSA", "TERRAS DE TRÁS-OS-MONTES", 
    "VISEU DÃO LAFÕES"), MATHMEAN = c(479.11, 530.23, 470.91, 499.72, 
    519.31, 460.5, 494.05, 497.15, 484.41, 461.79, 511.19, 503.52, 
    486.08, 509.93, 514.67, 504.39, 488.85, 461.74, 490.16, 490.46, 
    502.82, 507.6, 452.97, 454.03, 514.99), interv = structure(c(3L, 
    5L, 2L, 3L, 4L, 2L, 3L, 3L, 3L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 
    2L, 3L, 3L, 4L, 4L, 2L, 2L, 4L), .Label = c("[-Inf,450]", "(450,475]", 
    "(475,500]", "(500,525]", "(525, Inf]"), class = "factor"), color = structure(c(3L, 
    5L, 2L, 3L, 4L, 2L, 3L, 3L, 3L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 
    2L, 3L, 3L, 4L, 4L, 2L, 2L, 4L), .Label = c("red4", "red", "yellow", 
    "green", "green4"), class = "factor")), .Names = c("id", "MATHMEAN", 
    "interv", "color"), row.names = c(NA, -25L), class = "data.frame")
    

2 个答案:

答案 0 :(得分:0)

尝试添加:

names(colors) <- niv_leg

ggplot来电之前。如果没有名称,映射顺序可能会有所不同,这就是导致问题的原因(我相信)。我也会尝试运行它而不先设置标签,以确保它们以相同的顺序通过。

如果您添加要在数据中读取的代码(例如使用dput)以及如何加载/准备shapefile,我可以直接使用解决方案。

答案 1 :(得分:0)

按照@hrbrmstr的建议,我相信我得到了一个有效的解决方案:添加

scale_fill_identity("Média", labels = niv_leg, breaks = colors,
                  guide = "legend")

geo_map(...)

之后的某个地方

谢谢大家的反馈。我只是想知道为什么scale_fill_manual弄乱了颜色...我想这是将颜色解释为一个因素,而不是颜色的真实名称......