Creating Custom Maps with rworldmap: Adding Points for City Locations

Adding Points to Represent Cities on a World Map using rworldmap

Introduction

In this article, we will explore how to add points to represent cities on a world map using the rworldmap package in R. We will delve into the details of creating custom maps and adding geographical features such as countries, states, and cities.

Understanding rworldmap

The rworldmap package provides an interface to the Natural Earth map data, which is a popular dataset for geospatial analysis. The package allows users to create custom maps by merging their own data with the underlying map data.

Creating a Custom Map

To start, we need to load the required libraries and create a data frame containing the country names:

library(rworldmap)
library(ggplot2)

# Create a data frame containing the country names
theCountries <- c("USA", "CAN", "DEU", "FRA", "IND",
                 "GBR", "NLD", "ITA", "CHN", "KOR", "JPN",
                 "ESP", "PRT", "RUS", "NOR", "SGP", "AUS",
                 "CHL", "MEX", "PHL", "RWA", "JOR", "USA")

Next, we use the joinCountryData2Map function to merge our country names with the underlying map data:

# Merge the country names with the underlying map data
CEAcountries <- joinCountryData2Map(theCountries, joinCode = "ISO3",
                              nameJoinColumn = "country")

Now that we have a merged data frame, we can use the mapCountryData function to create our custom map:

# Create a custom map using the mapCountryData function
mapCountryData(CEAcountries, nameColumnToPlot="country", catMethod = "categorical",
               mapTitle='CEA Locations', missingCountryCol = gray(.8))

Adding Points to Represent Cities

To add points to represent cities on our custom map, we need to get the location data of the cities. We can use the maps package to access the world.cities dataset:

# Load the maps package
library(maps)

# Get the location data of the cities
data("world.cities")

# Filter the city locations to only include capitals
plotcities <- subset(world.cities, capital == 1)

Now that we have the city locations, we can add points to our custom map using the points function:

# Add points to represent cities on the custom map
points(plotcities$long, plotcities$lat, pch = 18, col = "black")

Customizing the Map

To further customize our map, we can use various options available in the mapCountryData function. For example, we can add a legend to our map:

# Add a legend to the custom map
mapCountryData(CEAcountries, nameColumnToPlot="country", catMethod = "categorical",
               mapTitle='CEA Locations', missingCountryCol = gray(.8), addLegend = TRUE)

Conclusion

In this article, we have explored how to add points to represent cities on a world map using the rworldmap package in R. We have also touched upon customizing the map by adding geographical features such as countries and states.

Example Use Cases

Here are some example use cases for creating custom maps with the rworldmap package:

  • Creating a country-by-country analysis of economic indicators
  • Visualizing population density across different regions
  • Identifying trends in environmental data

Packages to Consider

If you’re looking for alternative packages that offer more advanced mapping capabilities, consider checking out:

  • tmap: A powerful package for creating interactive maps.
  • ggplot2: A popular data visualization library that includes mapping capabilities.

Conclusion

In conclusion, adding points to represent cities on a world map using the rworldmap package in R is a straightforward process. By merging your own data with the underlying map data and using various options available in the mapCountryData function, you can create custom maps tailored to your specific needs.


Last modified on 2024-04-20