根据变量值向国家和大洲填充地图库 [英] Filling countries and continents with maps library according to variable value

查看:111
本文介绍了根据变量值向国家和大洲填充地图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的变量 prob2 值,我想用UE填充一些国家/地区,然后填写亚洲和非洲等大洲的地图。这是我的数据 map_d

  state prob2 
< ; chr> < dbl>
德国0.6
奥地利2.9
比利时1.9
保加利亚0.6
塞浦路斯0.0
克罗地亚1.7
...
其他亚洲人9.2
其他非洲人2.5
其他北美人10.7
其他拉丁美洲人2.3
其他大洋洲5.0

首先,我使用以下代码填充欧洲国家/地区:

  europ_map<-map_data (世界,区域= c(
德国,
奥地利,
比利时,
保加利亚,
Chipre,
克罗地亚,
丹麦,
斯洛伐克,
斯洛文尼亚,
西班牙,
爱沙尼亚,
芬兰,
法国,
希腊,
匈牙利,
爱尔兰,
意大利,
拉脱维亚,
立陶宛,
卢森堡,
马耳他,
挪威,
荷兰,
波兰 ,
葡萄牙,
英国,
捷克共和国,
罗马尼亚,
瑞典))

fin_map<-merge(europ_map,map_d,by.x = region,by.y = state)
库(plyr)
fin_地图<-排列(fin_map,group,order)

ggplot(fin_map,aes(x = long,y = lat,group = group,fill = prob2))+
geom_polygon( color = white)+
coord_map( polyconic)

会生成此地图:



您必须将大洲映射到数据库,并将数据库中的国家/地区名称映射到从 map_data 。之后,在代码中添加 + geom_polygon(aes(x = long,y = lat,group = group,fill = prob2),wm,color = NA)


I want to fill a map with some countries from UE, and then some continents like Asia and Africa, according to my variable prob2 values . This is my data map_d:

state prob2
<chr> <dbl>
Germany   0.6
Austria   2.9
Belgium   1.9
Bulgaria   0.6
Cyprus   0.0
Croatia   1.7
...
Other Asian   9.2
Other African   2.5
Other North American  10.7
Other Latin American   2.3
Other Oceania   5.0

Firstly I fill the countries of Europe, using this code:

europ_map <- map_data("world", region = c(
  "Germany", 
"Austria",
"Belgium",
"Bulgaria",
"Chipre",
"Croacia",
"Denmark",
"Slovakia",
"Slovenia",
"Spain",
"Estonia",
"Finland",
"France",
"Greece",
"Hungary",
"Ireland",
"Italy",
"Latvia",
"Lithuania",
"Luxembourg",
"Malta",
"Norway",
"Netherlands",
"Poland",
"Portugal",
"UK",
"Czech Republic",
"Romania",
"Sweden"))

fin_map <- merge(europ_map, map_d, by.x="region", by.y="state")
library(plyr)
fin_map <- arrange(fin_map, group, order)

ggplot(fin_map, aes(x=long, y=lat, group=group, fill=prob2)) +
  geom_polygon(colour = "white") +
  coord_map("polyconic")

Which produces this map: Europe Map

Now, I need to add shape of continents to my map, and fill with the value of prob2. Is possible?

I found in this post how to plot continents but is a different way to do: David Ameller's question, and Im not able to add variable values throught this code.

Thanks in advance!!

解决方案

FWIW, here's a starter:

library(tidyverse)
wm <- map_data("world")
cc <- raster::ccodes()
head(cc[,c(1:3, 8:10)], 3)
#          NAME ISO3 ISO2     UNREGION1 UNREGION2     CONTINENT
# 1       Aruba  ABW   AW     Caribbean  Americas South America
# 2 Afghanistan  AFG   AF Southern Asia      Asia          Asia
# 3      Angola  AGO   AO Middle Africa    Africa        Africa
dat <- read.csv(text="state, prob2
Other Asian,   9.2
Other African,   2.5
Other North American,  10.7
Other Latin American,   2.3
Other Oceania,   5.0")
mappings <- c("Asia"="Other Asian", "Africa"="Other African") # you add the others here
cc$MYCONTINENTS <- mappings[cc$CONTINENT]
cc <- left_join(cc, dat, by = c("MYCONTINENTS"="state"))

## 31 country names need to be mapped... 
wm$region %>% unique %>% setdiff(cc$NAME)
# ...                        
# [7] "Canary Islands"  "UK"  "Heard Island"     
# ...
## For example, UK is called United Kingdom in cc:
unique(grep("Kingdom", cc$NAME, value=T, ignore.case=T))
# [1] "United Kingdom"

mappings <- c("UK"="United Kingdom", "USA"="United States") # You add the others here
cc$NAME[match(mappings, cc$NAME)] <- names(mappings)

wm <- left_join(wm, cc[,c("NAME","MYCONTINENTS", "prob2")], by=c("region"="NAME"))
ggplot() +
  geom_polygon(aes(x=long, y=lat, group=group, fill=prob2), wm, colour = NA) +
  coord_quickmap()

You'll have to map your continents to the database ones, and the country names in the database to the ones you get from map_data. After that, add + geom_polygon(aes(x=long, y=lat, group=group, fill=prob2), wm, colour = NA) to your code.

这篇关于根据变量值向国家和大洲填充地图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆