Search code examples
rrworldmap

Adding points to represent cities on a World Map using rworldmap


I want to make a world map with specific countries filled in, but if possible I'd like to also include pinpoints representing specific cities on the countries.

I've been using the rworldmap package and experimenting with others but I'm having a hard time understanding how to pinpoint specific city locations and where to include the mapbubbles for the pinpoint.

Maybe I could add a line like this, representing San Diego, but I don't know where it would go:

mapBubbles(dF="CEAMap", nameX = "-117.16", nameY = "32.71", nameZSize = "CEAMap",)

I've been reading the developer's PDF and I honestly can't really understand how to implement the map bubbles and what things like nameZsize are supposed to contain in the first place.

I used this guide to learn how to highlight the countries, but as far as pinpointing the cities, it looks like I'm on my own because I can't really understand the developer's PDF. How to create a world map in R with specific countries filled in?

Here is my full code so far:

library(rworldmap)
library(ggmap)
library(maptools)
library(maps)

theCountries <- c("USA", 
                  "CAN", "DEU", "FRA", "IND", 
                  "GBR", "NLD", "ITA", 
                  "CHN", "KOR", "JPN", 
                  "ESP", "PRT", "RUS", 
                  "NOR", "SGP", "AUS", 
                  "CHL", "MEX", "PHL", "RWA", 
                  "JOR", "HND", "PAN", "THA", "DOM", 
                  "ZAF", "TUR", "CHE", "FIN",
                  "SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS", 
                  "UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
                  "VNM", "NGA", "DNK", "IRN", "AFG")
# These are the ISO3 names of the countries you'd like to plot in red

CEAMap <- data.frame(country = c("USA", 
                                 "CAN", "DEU", "FRA", "IND", 
                                 "GBR", "NLD", "ITA", 
                                 "CHN", "KOR", "JPN", 
                                 "ESP", "PRT", "RUS", 
                                 "NOR", "SGP", "AUS", 
                                 "CHL", "MEX", "PHL", "RWA", 
                                 "JOR", "HND", "PAN", "THA", "DOM", 
                                 "ZAF", "TUR", "CHE", "FIN",
                                 "SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS", 
                                 "UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
                                 "VNM", "NGA", "DNK", "IRN", "AFG"),
                      involvement = c(1, 
                                    2, 2, 2, 2, 
                                    3, 3, 3, 
                                    4, 4, 4, 
                                    5, 5, 5, 
                                    6, 6, 6, 
                                    7, 7, 7, 7,
                                    8, 8, 8, 8, 8,
                                    9, 9, 9, 9,
                                    10, 10, 10, 10, 10, 10, 10, 10, 
                                    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
                                    12, 12, 12, 12, 12))

# CEAMap is a data.frame with the ISO3 country names plus a variable to
# merge to the map data

CEAcountries <- joinCountryData2Map(CEAMap, joinCode = "ISO3",
                              nameJoinColumn = "country")


# This will join your CEAMap data.frame to the country map data

mapCountryData(CEAcountries, nameColumnToPlot="country", 
               catMethod = "categorical",
               mapTitle='CEA Locations',
               missingCountryCol = gray(.8))

I'd like to have specific points highlighting cities on the filled in countries.


Solution

  • I'd like to have specific points highlighting cities on the filled in countries.

    Then mapBubbles()is the wrong function. You can just add points() as in a regular R-plot.

    Get the location data of the cities before:

    library(maps)
    
    data("world.cities")
    
    plotcities <- subset(world.cities, capital == 1)
    

    I don't know which cities are of interest to you, so I took just the capitals

    mapCountryData(CEAcountries, nameColumnToPlot="country", 
                   catMethod = "categorical",
                   mapTitle='CEA Locations',
                   missingCountryCol = gray(.8), addLegend = FALSE)
    points(plotcities$long, plotcities$lat, pch =  18, col = "black")
    
    

    enter image description here

    I recommend looking at packages like tmap though. They make much nicer maps. ggplot2 might be a better choice, too.

    EDIT: To use the citys you provided just select accordingly:

    plotcities <- subset(world.cities, 
                         name %in% c("Cologne", "Chennai", "Denver", "Madrid", "Manila", "San Diego", "Seattle", "Shanghai")
                         & country.etc %in% c("Germany", "USA", "Spain", "China", "Philippines", "India"))