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

R 使用ggplot2绘制彩色编码的世界地图,r,plot,ggplot2,rworldmap,R,Plot,Ggplot2,Rworldmap,我正在尝试生成一幅世界地图,其中每个国家的颜色对应于存储在数据框中的特定值 > aggregated_country_data country num_responses region 1 AL 1 Albania 2 AM 1

我正在尝试生成一幅世界地图,其中每个国家的颜色对应于存储在数据框中的特定值

> aggregated_country_data
   country num_responses                                     region
1       AL             1                                    Albania
2       AM             1                                    Armenia
3       AR            32                                  Argentina
...
75      ZW             3                                   Zimbabwe
这就是我尝试过的

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
库(rworldmap)
图书馆(GG2)
map.world
库(rworldmap)
图书馆(GG2)

世界地图,你确定吗?在我的机器上看起来不错也许这篇文章很有用[link]()@mucio它用颜色绘制地图,但国家的颜色代码与“num_responses”列中的数字不匹配,它们似乎与国家代码匹配。但是我会再试一次。可能使用
fill=factor(num\u responses)
否则颜色可能太相似。我尝试过,它根本不显示与num\u responses向量相关的颜色。我完全搞糊涂了。也许移除南极洲,至少使用
coord\u map()
?@hrbrmstr我最初的方法是在map\u data()中使用projection=“mercator”,然后使用coord\u map()。但是在map_data()中将projection参数设置为“mercator”会出现一个错误:“名称中的错误[df$group,1]:下标超出边界”。知道有什么问题吗?干杯
col
向量未使用,对吗?请删除它好吗?请注意map.world对象中的国家名称不是标准的(例如,俄罗斯联邦显示为俄罗斯),这使得它很难连接到其他数据源(我尝试的214个名称中有74个是非标准的)@user2345448您好,您应该使用国家ISO代码将map.world数据关联到其他数据源,执行连接等,而不是国家名称;-)。
gg <- gg + geom_map(data=aggregated_country_data, map=map.world, aes(map_id=region, fill=num_responses), color="white", size=0.25)
gg
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