*Guest blog by Michael Grogan.*

Here is how we can use the **maps**, **mapdata** and **ggplot2** libraries to create maps in R.

In this particular example, we’re going to create a world map showing the points of Beijing and Shanghai, both cities in China. For this particular map, we will be displaying the Northern Hemisphere from Europe to Asia.

require(maps)

require(mapdata)

library(ggplot2)

library(ggrepel)

*cities = c("Beijing","Shanghai")*

*global <- map_data("world") ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group = group)) + coord_fixed(1.3)*

*ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group = group), fill = NA, color = "red") + coord_fixed(1.3)*

*gg1 <- ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group = group), fill = "green", color = "blue") + coord_fixed(1.3)gg1*

*coors <- data.frame( long = c(122.064873,121.4580600), lat = c(36.951968,31.2222200),stringsAsFactors = FALSE)*

**#xlim and ylim can be manipulated to zoom in or out of the map**

*coors$cities <- cities gg1 + geom_point(data=coors, aes(long, lat), colour="red", size=1) +ggtitle("World Map") +geom_text_repel(data=coors, aes(long, lat, label=cities)) + xlim(0,150) + ylim(0,100)*

Upon running this code, here is our map…

A few points to note:

- The "cities" variable is used to specify the labels for the cities.
- The "coors" data frame is used to define the latitude and longitude for each city.
- The xlim and ylim under ggplot is used to zoom in or out of the map, depending on the coordinates we set.

Note that we are also using the **ggrepel** library in order to space out the labels on the points for each city. Were this library not to be incorporated, then the labels have the potential to overlap each other, and it doesn't look very visually appealing...

But...what if I want maps that zoom in on a country?

cities = c("Paris","Berlin")

coors <- data.frame(

lat = c(48.864716,52.520008),

long = c(2.349014,13.404954),

stringsAsFactors = FALSE

)

coors$cities <- cities

gg1 +

geom_point(data=coors, aes(long, lat), colour="red", size=1) +

ggtitle("World Map") +

geom_text_repel(data=coors, aes(long, lat, label=cities)) + xlim(-10,40) + ylim(35,60)

As mentioned, xlim and ylim are set to a narrower margin. Here, xlim is set to (-10,40) and ylim is set to (35,60). However, in the previous map xlim was set to (0,150) and ylim was set to (0,100).

Note that because this method is using a world map database, you might often find that the countries surrounding the ones we want (in this case, France and Germany) appear somewhat "broken up". This may not be an issue if you are simply looking to represent a particular country, but you could also choose to plot one country in isolation, e.g. specifying map_data("usa") instead of map_data("world").

© 2020 Data Science Central ® Powered by

Badges | Report an Issue | Privacy Policy | Terms of Service

**Upcoming DSC Webinar**

- Optimization and The NFL’s Toughest Scheduling Problem - June 23

At first glance, the NFL’s scheduling problem seems simple: 5 people have 12 weeks to schedule 256 games over the course of a 17-week season. The scenarios are potentially well into the quadrillions. In this latest Data Science Central webinar, you will learn how the NFL began using Gurobi’s mathematical optimization solver to tackle this complex scheduling problem. Register today.

**Most Popular Content on DSC**

To not miss this type of content in the future, subscribe to our newsletter.

- Book: Statistics -- New Foundations, Toolbox, and Machine Learning Recipes
- Book: Classification and Regression In a Weekend - With Python
- Book: Applied Stochastic Processes
- Long-range Correlations in Time Series: Modeling, Testing, Case Study
- How to Automatically Determine the Number of Clusters in your Data
- New Machine Learning Cheat Sheet | Old one
- Confidence Intervals Without Pain - With Resampling
- Advanced Machine Learning with Basic Excel
- New Perspectives on Statistical Distributions and Deep Learning
- Fascinating New Results in the Theory of Randomness
- Fast Combinatorial Feature Selection

**Other popular resources**

- Comprehensive Repository of Data Science and ML Resources
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- 100 Data Science Interview Questions and Answers
- Cheat Sheets | Curated Articles | Search | Jobs | Courses
- Post a Blog | Forum Questions | Books | Salaries | News

**Archives:** 2008-2014 |
2015-2016 |
2017-2019 |
Book 1 |
Book 2 |
More

**Upcoming DSC Webinar**

- Optimization and The NFL’s Toughest Scheduling Problem - June 23

At first glance, the NFL’s scheduling problem seems simple: 5 people have 12 weeks to schedule 256 games over the course of a 17-week season. The scenarios are potentially well into the quadrillions. In this latest Data Science Central webinar, you will learn how the NFL began using Gurobi’s mathematical optimization solver to tackle this complex scheduling problem. Register today.

**Most popular articles**

- Free Book and Resources for DSC Members
- New Perspectives on Statistical Distributions and Deep Learning
- Time series, Growth Modeling and Data Science Wizardy
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- Comprehensive Repository of Data Science and ML Resources
- Advanced Machine Learning with Basic Excel
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- Selected Business Analytics, Data Science and ML articles
- How to Automatically Determine the Number of Clusters in your Data
- Fascinating New Results in the Theory of Randomness
- Hire a Data Scientist | Search DSC | Find a Job
- Post a Blog | Forum Questions

## You need to be a member of Data Science Central to add comments!

Join Data Science Central