我呼籲區域這個數據幀:Ploting正確的顏色與ggplot和geom_map
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")
但我面臨兩個問題,我不能數字出來:
- 我只得到4個標籤圖例(他們應該是5)
- 顏色不正確ploted在所有不同的「ID」的區域...
的「身份證」在mapaf匹配區域數據框中的「id」,所以我不知道爲什麼它不工作...任何幫助將不勝感激。
PS: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")
嘗試'scale_fill_identity()' – hrbrmstr
你應該也真的簡化了多邊形。這是一個高層次的choropleth,你會加快渲染 – hrbrmstr
謝謝hrbrmstr。添加'scale_fill_identity()'給我正確的顏色。但是我放鬆了定製的傳說...會看看幫助,看看是否有解決方法?我確實使用'fortify'來簡單地顯示地圖數據......你是指別的東西? – JPMD