【问题标题】:Filling countries and continents with maps library according to variable value根据变量值用地图库填充国家和大陆
【发布时间】:2017-10-03 16:54:21
【问题描述】:

根据我的变量prob2 values,我想用 UE 的一些国家/地区填充地图,然后是亚洲和非洲等一些大陆。这是我的数据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

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

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

生成这张地图: Europe Map

现在,我需要在地图中添加大陆形状,并填充 prob2 的值。有可能吗?

我在这篇文章中发现了如何绘制大陆,但这是一种不同的方法:David Ameller's question,我无法通过此代码添加变量值。

提前致谢!!

【问题讨论】:

    标签: r ggplot2 colors maps rworldmap


    【解决方案1】:

    FWIW,这是一个首发:

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

    您必须将您的大陆映射到数据库中,并将数据库中的国家名称映射到您从map_data 获得的那些。之后,将+ geom_polygon(aes(x=long, y=lat, group=group, fill=prob2), wm, colour = NA) 添加到您的代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      相关资源
      最近更新 更多