![](/img/trans.png)
[英]Creating maps in R just like the way rworldmap does but for specific country with provinces
[英]custom color for provinces in specific country using raster package in R
我正在嘗試標記(綠色,紅色,藍色)特定國家/地區的每個省,如下所示:
library(raster)
library(rgeos)
library(ggplot2)
library(dplyr)
iran <- getData("GADM", country = "Iran", level = 1)
map <- fortify(iran)
map$id <- as.integer(map$id)
dat <- data.frame(id = 1:(length(iran@data$NAME_1)),
state = iran@data$NAME_1,
pr = c(530, -42, 1673, 75, 206, 544, 1490, 118, 75,
40, 105, 191, 111, 810, 609, 425, 418, 550, 40, 425, -54, -50,
16, 18, 133,425, -30, 241,63, 191,100))
dat <- dat %>% mutate(color_province = case_when(pr <= 50 ~ 'green',
pr > 150 ~ 'red',
TRUE ~ 'yellow'))
我想為每個省設置自定義顏色(使用 'dat' 數據框)如下:
> dat
id state pr color_province
1 1 Alborz 530 red
2 2 Ardebil -42 green
3 3 Bushehr 1673 red
4 4 Chahar Mahall and Bakhtiari 75 yellow
5 5 East Azarbaijan 206 red
6 6 Esfahan 544 red
7 7 Fars 1490 red
8 8 Gilan 118 yellow
9 9 Golestan 75 yellow
10 10 Hamadan 40 green
11 11 Hormozgan 105 yellow
12 12 Ilam 191 red
13 13 Kerman 111 yellow
14 14 Kermanshah 810 red
15 15 Khuzestan 609 red
16 16 Kohgiluyeh and Buyer Ahmad 425 red
17 17 Kordestan 418 red
18 18 Lorestan 550 red
19 19 Markazi 40 green
20 20 Mazandaran 425 red
21 21 North Khorasan -54 green
22 22 Qazvin -50 green
23 23 Qom 16 green
24 24 Razavi Khorasan 18 green
25 25 Semnan 133 yellow
26 26 Sistan and Baluchestan 425 red
27 27 South Khorasan -30 green
28 28 Tehran 241 red
29 29 West Azarbaijan 63 yellow
30 30 Yazd 191 red
31 31 Zanjan 100 yellow
例如,'Ardebil' 省是綠色的,'Zanjan' 省是黃色的。 我使用“填充”參數來設置自定義顏色,這是我的嘗試:
map_df <- inner_join(map, dat, by = "id")
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state
ggplot() +
geom_map(data = map_df, map = map_df,
aes(map_id = id, group = group,
x = long, y = lat,
fill = as.factor(color_province))) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 3) +
coord_map() +
labs(x = "", y = "", title = "Iran Province") +
scale_fill_manual(values = list(yellow = 'yellow', red = 'red', green = 'green'))
但它不起作用。 根據“dat”數據框,“Ardebil”是“綠色”,但在地圖上是紅色的。 有一個更好的方法嗎?
既然你問了我,我就快速瀏覽了你的代碼。 你對id
假設基本上是錯誤的。 當您使用fortify()
,您可能認為id
是按正常順序分配的(例如,1 到 n)。 但事實並非如此。 只需運行unique(mymap$id)
。 你會明白我的意思。 那么解決方案是什么? 創建dat
,您需要rownames(iran@data)
。 一旦完成,你應該沒問題。 查看最終圖形。
library(raster)
library(rgeos)
library(ggplot2)
library(dplyr)
iran <- getData("GADM", country = "Iran", level = 1)
mymap <- fortify(iran) # This is the hidden cause
mymap$id <- as.integer(mymap$id)
dat <- data.frame(id = rownames(iran@data), # This is what you needed.
state = iran@data$NAME_1,
pr = c(530,-42,1673,75,206,544,1490,118,75,
40,105,191,111,810, 609,425,418,550, 40, 425, -54,-50,
16, 18, 133,425, -30, 241,63, 191,100)) %>%
mutate(color_province = case_when(pr <= 50 ~ 'green',
pr > 150 ~ 'red',
TRUE ~ 'yellow'))
mydf <- inner_join(mymap, dat, by = "id")
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state
ggplot() +
geom_map(data = mydf,
map = mydf,
aes(map_id = id, group = group,
x = long, y = lat,
fill = as.factor(color_province))) +
geom_text(data = centers,
aes(label = state, x = x, y = y), size = 3) +
coord_map() +
labs(x = "", y = "", title = "Iran Province") +
scale_fill_manual(values = c("green", "red", "yellow"),
name = "Province")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.