2
votes

I am using the global terrorism data set from Kaggle, and I'm trying to plot on a world map (or just Europe and neighboring countries). I need to plot the terror attacks as points: size - number killed. I also need to color the countries according to the number of immigrants in them - that I would require an input from a different data set. So far what I have got is:

df <- read.csv("C:.../globalterrorismdb_0617dist.csv", stringsAsFactors = FALSE)

locs <- data.frame(lon = df$longitude, lat = df$latitude, stringsAsFactors = FALSE)

nMap <- get_map("Vienna, Austria", zoom = 4, maptype = "toner", source = "stamen")

ggmap(nMap) + 
  geom_point(aes(x = locs$lon, y = locs$lat ,color = df$nkill), data = df) + 
  geom_point(size = 6, alpha = 0.3)

which gives me: Terror attacks locations

The important part of the attacks data looks like:

>    Country,                 latitude,          longitude  
>    United Kingdom           54.59727            -5.930109 
>    Spain                    43.18418            -2.473289 
>    Turkey                   37.95032            27.373266 
>    Italy                    41.89052            12.494249
>    United Kingdom           54.22641            -7.649053

Now I would like as mentioned to color countries according to the number of refugees and/or immigrants. For that purpose, lets assume I have a data set which contains that in the form of (fake numbers):

>    country,                 # 
>    United Kingdom           123 
>    Spain                    100  
>    Turkey                   250 
>    Italy                    80

Please help.

1
You want to provide a sample data. Otherwise nobody knows how your data is like; you are unlikely to receive a help from SO users. In addition, your question will become a reference for others in the future. You want to help them so that they can learn from your case. Please revise your question adding your sample data. - jazzurro
I did that now. Hopefully it is fine - HermanK

1 Answers

3
votes

Here is one approach for overlaying country polygons on the base map provided by ggmap::get_map:

  1. Download the desired background map using ggmap::get_map. Note that the structure returned by this function includes the bounding box as an attribute, which can be accessed as attr(nMap, "bb") (where nMap is the object returned by get_map). It will be needed later.
  2. Get country map data using ggplot2::map_data("world") and join any additional variables from the country-level dataset, e.g., using merge or dplyr's join functions.
  3. Because some countries may be partially in the bounding box, you will need to clip the polygons to the bounding box. The function PBSmapping::clipPolys provides this capability.
  4. Use geom_polygon to overlay the country polygons on the base map.

Sample code and output follow.

library(ggmap)
library(ggplot2)
library(dplyr)
library(PBSmapping) # to clip polygons
require(ggthemes) # for theme_map, if desired

# define data (a simple dataset is constructed here
# for illustration purposes) and background map
countryData<-data.frame(region=factor(c("France", "Germany", "Libya")), data=c(2, 15, 1))
nMap <- get_map("Vienna, Austria",zoom=4,maptype="toner",source="stamen")

#get country polygon data
mapdata <- map_data("world")
mapdata <- left_join(mapdata, countryData, by="region")

#get bounding box for map
bb<-attr(nMap, "bb");
ylim<-c(bb$ll.lat, bb$ur.lat)
xlim<-c(bb$ll.lon, bb$ur.lon)

#clip polygons to map
colnames(mapdata)[1:6] <- c("X","Y","PID","POS","region","subregion")
mapdata<-clipPolys(mapdata, xlim=xlim, ylim=ylim, keepExtra=TRUE)

#plot map overlay
ggmap(nMap)+coord_map(xlim=xlim,ylim=ylim) +
    geom_polygon(data=mapdata, aes(x=X, y=Y, group=PID, fill=data), alpha=0.5) +
    ggthemes::theme_map()

choropleth output