简体   繁体   English

使用ggplot2绘制颜色编码的世界地图

[英]Plot colour coded world map using ggplot2

I am trying to generate a generate a plot of a world map where the colour of each country corresponds to a particular value stored in a data frame. 我正在尝试生成生成世界地图的图,其中每个国家/地区的颜色对应于存储在数据框中的特定值。

> aggregated_country_data
   country num_responses                                     region
1       AL             1                                    Albania
2       AM             1                                    Armenia
3       AR            32                                  Argentina
...
75      ZW             3                                   Zimbabwe

This is what I have tried 这就是我尝试过的

library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")

gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat), fill="white", colour="black", size=0.25)
gg

That plots the world map just fine, so next I want to add colour to each country in proportion to the value 'num_responses' in aggregated_country_data 这绘制了世界地图就好了,接下来我想为每个国家添加颜色,与aggregated_country_data中的'num_responses'值成比例

gg <- gg + geom_map(data=aggregated_country_data, map=map.world, aes(map_id=region, fill=num_responses), color="white", size=0.25)
gg

But now it's colour coding each of the colours as they correspond to the country code rather than the value that's in the column num_responses in aggregated_country_data. 但是现在它对每种颜色进行颜色编码,因为它们对应于国家/地区代码,而不是在aggregated_country_data中列num_responses中的值。

It's clear that there's something about ggplot2 that I'm not getting, but I can't figure out what that is. 很明显,我没有得到关于ggplot2的东西,但我无法弄清楚那是什么。

I would appreciate any input, Brad 我很感激任何投入,布拉德


I figured out what the problem was, and it has nothing to do with ggplot2 or anything else that I was doing. 我弄清楚问题是什么,它与ggplot2或我正在做的任何其他事情无关。 The aggregated_country_data data frame has different names for 'region' than in map.world. aggregated_country_data数据框的“region”名称与map.world中的名称不同。 My input data (aggregated_country_data) uses a two letter country code by default that I converted into country name (called 'region' in the data frame) using the countrycode R package, but it is using a different naming convention for the names than exists in map.world. 我的输入数据(aggregated_country_data)默认使用两个字母的国家代码,我使用国家代码R包转换为国家名称(在数据框中称为“区域”),但它使用的名称的命名约定与map.world。 So that's a totally different problem. 所以这是一个完全不同的问题。

library(rworldmap)
library(ggplot2)
map.world <- map_data(map="world")

#Add the data you want to map countries by to map.world
#In this example, I add lengths of country names plus some offset
map.world$name_len <- nchar(map.world$region) + sample(nrow(map.world))

gg <- ggplot()
gg <- gg + theme(legend.position="none")
gg <- gg + geom_map(data=map.world, map=map.world, aes(map_id=region, x=long, y=lat, fill=name_len))

gg <- gg + scale_fill_gradient(low = "green", high = "brown3", guide = "colourbar")
gg <- gg + coord_equal()
gg

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM