大陆名称和区域地图

huangapple go评论65阅读模式
英文:

Continent names and choropleth maps

问题

我正在使用各国的ISO-3名称。以下是一个示例。我想要将这些数据用于分级填充地图。

datar <- data.frame(GEO = c("GR","AZ","TR","GA","IR"),
                    Value = c(0.5560,0.323,0.2140,0.312,0.0225),
                    search_countries = factor(c("Greece","Azerbaijan","Turkey","Georgia","Iran")),
                    countries = c("Greece","Azerbaijan","Turkey","Georgia","Iran")
)

map <- map_data('world', region = datar$search_countries)

代码的最后一行允许我绘制分级填充地图。现在我想在表格中的每个国家旁边添加一个大洲名称的附加列,放在子区域列的旁边。

英文:

I am working with ISO-3 names of the countries. Below you can see one example.I want to use this data for Choropleth map.

    datar &lt;- data.frame(GEO = c(&quot;GR&quot;,&quot;AZ&quot;,&quot;TR&quot;,&quot;GA&quot;,&quot;IR&quot;),
                        Value = c(0.5560,0.323,0.2140,0.312,0.0225),
                        search_countries = factor(c(&quot;Greece&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)),
                        countries = c(&quot;Greece&quot;,&quot;Azerbaijan&quot;,&quot;Turkey&quot;,&quot;Georgia&quot;,&quot;Iran&quot;)
    )
    
map &lt;- map_data(&#39;world&#39;, region = datar$search_countries)

The code's last line allows me to draw a Choropleth map. Now I want to have an additional column with the continent names for each country next to the column subregion.

大陆名称和区域地图

So can anybody help me how to solve this problem and to have a continent name for each country on the table?

答案1

得分: 1

如果我理解正确,您想要在数据框中为每个国家添加一个包含相应洲际的列?

在这里,我们可以使用raster包获取国家和洲际名称,然后将它们与您的数据框合并 大陆名称和区域地图

country.codes <- raster::ccodes()

map.continents <- left_join(map, country.codes, by = c('region' = 'NAME')) %>%
  select(all_of(colnames(map)), continent)
英文:

If I understand correctly you want to add a column containing the relevant continents for each country in your dataframe?

Here we can get the country and continent names using the raster package then join them onto your dataframe 大陆名称和区域地图

country.codes &lt;- raster::ccodes()

map.continents &lt;- left_join(map, country.codes, by = c(&#39;region&#39; = &#39;NAME&#39;)) %&gt;% 
  select(all_of(colnames(map)), continent)

huangapple
  • 本文由 发表于 2023年2月6日 18:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定