Choropleth world map

后端 未结 2 1269
半阙折子戏
半阙折子戏 2021-02-01 11:51

I have read so many threads and articles and I keep getting errors. I am trying to make a choropleth? map of the world using data I have from the global terrorism database. I wa

2条回答
  •  爱一瞬间的悲伤
    2021-02-01 12:08

    Building on the nice work by @jlhoward. You could instead use rworldmap that already has a world map in R and has functions to aid joining data to the map. The default map is deliberately low resolution to create a 'cleaner' look. The map can be customised (see rworldmap documentation) but here is a start :

    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 )
    

    Following a comment from @hk47, you can also add the points to the map sized by the number of casualties.

    deaths <- subset(x=gtd, nkill >0)
    
    mapBubbles(deaths,
               nameX='longitude',
               nameY='latitude', 
               nameZSize='nkill', 
               nameZColour='black',
               fill=FALSE, 
               addLegend=FALSE, 
               add=TRUE)
    

提交回复
热议问题