简体   繁体   English

使用 R 中的栅格包为特定国家/地区的省份自定义颜色

[英]custom color for provinces in specific country using raster package in R

I am trying to label(green, red, blue) each province in a specific country as follows:我正在尝试标记(绿色,红色,蓝色)特定国家/地区的每个省,如下所示:

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'))

I want to set custom color to each province (using 'dat' data frame) as follows:我想为每个省设置自定义颜色(使用 '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

For example 'Ardebil' province is green and 'Zanjan' province is yellow.例如,'Ardebil' 省是绿色的,'Zanjan' 省是黄色的。 I used 'Fill' argument to set custom color and here is my try:我使用“填充”参数来设置自定义颜色,这是我的尝试:

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'))

在此处输入图片说明

But it does not work.但它不起作用。 According to the 'dat' data frame 'Ardebil' is 'green' but it is red on map.根据“dat”数据框,“Ardebil”是“绿色”,但在地图上是红色的。 Is there a better way to do this?有一个更好的方法吗?

Since you asked me, I had a quick look of your code.既然你问了我,我就快速浏览了你的代码。 Your assumption for id was basically wrong.你对id假设基本上是错误的。 When you used fortify() , you probably thought id was assigned in a normal sequence (eg, 1 to n).当您使用fortify() ,您可能认为id是按正常顺序分配的(例如,1 到 n)。 But this was not the case.但事实并非如此。 Just run unique(mymap$id) .只需运行unique(mymap$id) You will see what I mean.你会明白我的意思。 So what was the solution?那么解决方案是什么? When you create dat , you need rownames(iran@data) .创建dat ,您需要rownames(iran@data) Once this is done, you should be fine.一旦完成,你应该没问题。 See the final graphic.查看最终图形。

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.

相关问题 在R中创建地图就像rworldmap的方式一样,但对于具有省份的特定国家/地区 - Creating maps in R just like the way rworldmap does but for specific country with provinces 确定R中的具体国家和省份 - Mapping specific States and Provinces in R R 中光栅 package 的默认调色板是什么? - What is the default color palette for the raster package in R? 如何为 r plot 从整个世界栅格和该特定国家的 shapefile 中的特定国家绘制 map? - How to draw map for specific country in r plot from a whole world raster and that specific country’s shapefile? 如何在R中使用栅格数据包显示栅格中的所有列名称 - How to show all column names in a raster using raster package in r 如何在 R 中为国家 map 着色特定区域? - How to color specific districts in a country map in R? 在水平图中指定NA栅格值的颜色(R包rasterVis) - Specifying color of NA raster values in levelplot (R package rasterVis) 使用R中的自定义颜色绘制光栅图像 - Plotting raster images using custom colours in R R栅格中的自定义函数由于zyp包中的回归而失败 - Custom function in R raster is failing from regression in zyp package 可视化特定国家的 MODIS 植被层(使用 R-package MODISttsp) - Visualising MODIS Vegetation layers for a specific country (using R-package MODISttsp)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM