Choropleth世界地图

时间:2014-03-25 03:51:33

标签: r ggplot2 maps choropleth

我已经阅读了很多线程和文章,并且我一直在收到错误。我试图做一个合唱?使用来自全球恐怖主义数据库的数据的世界地图。我想用一小部分颜色或者那个国家的攻击次数给国家上色。我现在不在乎。由于有这么多国家/地区存在数据,因此制作任何图表来显示此数据是不合理的。

非常感谢帮助,如果我没有正确地问这个,我真诚地道歉,我正在学习本网站的规则。

我的代码(到目前为止......)

library(maps)
library(ggplot2)
map("world")
world<- map_data("world")
gtd<- data.frame(gtd)
names(gtd)<- tolower(names(gtd))
gtd$country_txt<- tolower(rownames(gtd))
demo<- merge(world, gts, sort=FALSE, by="country_txt")

在gtd数据框中,countries列的名称为&#34; country_txt&#34;所以我想我会用它,但是我在fix.by(by.x,x)中得到错误:&#39; by&#39;必须指定唯一有效的列

如果这样可行,我会按照我在几个网站上看到的情节进行策划。 老实说,我已经在这方面工作了很长时间,我已经阅读了很多代码/其他类似的问题/网站/ r手册等等。我很乐意接受我不称职的时候很乐意提供一些帮助。

2 个答案:

答案 0 :(得分:8)

这样的东西?这是使用rgdalggplot的解决方案。我很久以前就放弃了使用基地R来做这类事情。

library(rgdal)        # for readOGR(...)
library(RColorBrewer) # for brewer.pal(...)
library(ggplot2)
setwd(" < directory with all files >")

gtd        <- read.csv("globalterrorismdb_1213dist.csv")
gtd.recent <- gtd[gtd$iyear>2009,]
gtd.recent <- aggregate(nkill~country_txt,gtd.recent,sum)
world      <- readOGR(dsn=".",
                      layer="world_country_admin_boundary_shapefile_with_fips_codes")

countries <- world@data
countries <- cbind(id=rownames(countries),countries)
countries <- merge(countries,gtd.recent, 
                   by.x="CNTRY_NAME", by.y="country_txt", all.x=T)
map.df <- fortify(world)
map.df <- merge(map.df,countries, by="id")
ggplot(map.df, aes(x=long,y=lat,group=group)) +
  geom_polygon(aes(fill=nkill))+
  geom_path(colour="grey50")+
  scale_fill_gradientn(name="Deaths",
                       colours=rev(brewer.pal(9,"Spectral")),
                       na.value="white")+
  coord_fixed()+labs(x="",y="")

全球恐怖主义数据库有多个版本。我使用了可用的完整数据集here,然后对年份进行了子集化&gt;因此,这张地图显示了从2010-01-01到2013-01-01(该来源提供的最新数据)按国家划分的恐怖主义死亡人数。这些文件以MS Excel下载的形式提供,我将其转换为csv以导入R。

world map可以作为GeoCommons website中的shapefile。

制作等值区域图的棘手部分是将数据与正确的多边形(国家/地区)相关联。这通常是一个四步过程:

  1. 在shapefile属性表中查找一个字段,该字段将(无双关语)映射到数据中的相应字段。在这种情况下,字段&#34; CNTRY_NAME&#34;在shapefile中映射到字段&#34; country_txt&#34;在gtd数据库中。
  2. 在ploygon ID(存储在属性表的行名称中)和CNTRY_NAME字段之间创建关联。
  3. 使用CNTRY_NAME和country_txt将结果与您的数据合并。
  4. 将结果与使用fortify(map)创建的数据框合并 - 这会将ploygons与死亡相关联(nkill)。

答案 1 :(得分:4)

以@jlhoward的优秀作品为基础。您可以改为使用已在R中具有世界地图的rworldmap,并具有帮助将数据连接到地图的功能。默认地图是故意低分辨率,以创建一个清洁&#39;看。地图可以自定义(参见rworldmap文档),但这是一个开始:

library(rworldmap)

#3 lines from @jlhoward 
gtd        <- read.csv("globalterrorismdb_1213dist.csv")
gtd.recent <- gtd[gtd$iyear>2009,]
gtd.recent <- aggregate(nkill~country_txt,gtd.recent,sum)

#join data to a map
gtdMap <- joinCountryData2Map( gtd.recent, 
                               nameJoinColumn="country_txt", 
                               joinCode="NAME" )

mapDevice('x11') #create a world shaped window

#plot the map
mapCountryData( gtdMap, 
                nameColumnToPlot='nkill', 
                catMethod='fixedWidth', 
                numCats=100 )

根据@ hk47的评论,您还可以根据伤亡人数将地图添加到地图中。

deaths <- subset(x=gtd, nkill >0)

mapBubbles(deaths,
           nameX='longitude',
           nameY='latitude', 
           nameZSize='nkill', 
           nameZColour='black',
           fill=FALSE, 
           addLegend=FALSE, 
           add=TRUE)

相关问题