Working with shapefiles, projections and world maps in ggplot

In this post I will show some different examples of how to work with map projections and how to plot the maps using ggplot. Many maps that are shown using their default projection are in the longlat-format, which is far from optimal. For plotting world maps I prefer to use either Robinson or Winkel Tripel projection—but many more are available—and I will show how to use both these projections.

Before we get started you need to download a couple of shapefiles that we will use. You can find them here:

Put them directly inside your working directory. We will use functions from the rgdal-package to read the shapefiles into R, so if you do not have it, you need to install it before you continue.

This will create a longlat-projected world map. World map in ggplot

Here the world map is shown using the Robinson projection. World map in ggplot with robinson projection

However, the Caspian sea is missing. This is because of how ggplot handles polygon holes. Ggplot will plot polygon holes as a separate polygon, thus we need to make it pseudo-transparent by changing its fill color.

![World map in ggplot2 polygon hole example][./img/map3.png]

Now the Caspian sea is visible. World map in ggplot polygon hole fix

If we want we can also add a graticule and a bounding box. The bounding box is useful if we want to make the sea blue—especially when using some form of curved projection. Here I have added a graticule and bounding box to the longlat-map.

World map in ggplot plus graticule and bounding box

Robinson projection with added graticule and bounding box. World map in ggplot using robinson projection with graticule and bounding box

Here I have added country borders to the previous map plot. World map in ggplot in robinson projection with country borders

Bubble plots are a popular way of displaying information on maps. Here I used project() to reproject the bubbles’ coordinates into the Robinson projection. World map in ggplot using robinson projection plus bubble plot

Lastly, here is an example of the Winkel tripel projection. This projection became popular after 1998 when the National Geographic Society choose to use it for their world maps—using it to replace the Robinson projection, which they previously used.
World map in ggplot using winkel tripel projection

Written by Kristoffer Magnusson, a researcher in clinical psychology. You should follow him on Twitter and come hang out on the open science discord Git Gud Science.


Published May 23, 2013 (View on GitHub)

Buy Me A Coffee

A huge thanks to the 79 supporters who've bought me a 179 coffees!

Jason Rinaldo bought ☕☕☕☕☕☕☕☕☕☕ (10) coffees

I've been looking for applets that show this for YEARS, for demonstrations for classes. Thank you so much! Students do not need to tolarate my whiteboard scrawl now. I'm sure they'd appreciate you, too.l

Someone bought ☕☕☕☕☕ (5) coffees

What a great site

@Daniel_Brad4d bought ☕☕☕☕☕ (5) coffees

Wonderful work!

David Loschelder bought ☕☕☕☕☕ (5) coffees

Terrific work. So very helpful. Thank you very much.

@neilmeigh bought ☕☕☕☕☕ (5) coffees

I am so grateful for your page and can't thank you enough!  

@giladfeldman bought ☕☕☕☕☕ (5) coffees

Wonderful work, I use it every semester and it really helps the students (and me) understand things better. Keep going strong.

Dean Norris bought ☕☕☕☕☕ (5) coffees

Sal bought ☕☕☕☕☕ (5) coffees

Really super useful, especially for teaching. Thanks for this! bought ☕☕☕☕☕ (5) coffees

Very helpful to helping teach teachers about the effects of the Good Behavior Game

@akreutzer82 bought ☕☕☕☕☕ (5) coffees

Amazing visualizations! Thank you!

@rdh_CLE bought ☕☕☕☕☕ (5) coffees

So good!

Someone bought ☕☕☕ (3) coffees

Ruslan Klymentiev bought ☕☕☕ (3) coffees

@lkizbok bought ☕☕☕ (3) coffees

Keep up the nice work, thank you!

@TELLlab bought ☕☕☕ (3) coffees

Thanks - this will help me to teach tomorrow!

SCCT/Psychology bought ☕☕☕ (3) coffees

Keep the visualizations coming!

@elena_bolt bought ☕☕☕ (3) coffees

Thank you so much for your work, Kristoffer. I use your visualizations to explain concepts to my tutoring students and they are a huge help.

A random user bought ☕☕☕ (3) coffees

Thank you for making such useful and pretty tools. It not only helped me understand more about power, effect size, etc, but also made my quanti-method class more engaging and interesting. Thank you and wish you a great 2021!

@hertzpodcast bought ☕☕☕ (3) coffees

We've mentioned your work a few times on our podcast and we recently sent a poster to a listener as prize so we wanted to buy you a few coffees. Thanks for the great work that you do!Dan Quintana and James Heathers - Co-hosts of Everything Hertz 

Cameron Proctor bought ☕☕☕ (3) coffees

Used your vizualization in class today. Thanks! bought ☕☕☕ (3) coffees

My students love these visualizations and so do I! Thanks for helping me make stats more intuitive.

Someone bought ☕☕☕ (3) coffees

Adrian Helgå Vestøl bought ☕☕☕ (3) coffees

@misteryosupjoo bought ☕☕☕ (3) coffees

For a high school teacher of psychology, I would be lost without your visualizations. The ability to interact and manipulate allows students to get it in a very sticky manner. Thank you!!!

Chi bought ☕☕☕ (3) coffees

You Cohen's d post really helped me explaining the interpretation to people who don't know stats! Thank you!

Someone bought ☕☕☕ (3) coffees

You doing useful work !! thanks !!

@ArtisanalANN bought ☕☕☕ (3) coffees


@jsholtes bought ☕☕☕ (3) coffees

Teaching stats to civil engineer undergrads (first time teaching for me, first time for most of them too) and grasping for some good explanations of hypothesis testing, power, and CI's. Love these interactive graphics!

@notawful bought ☕☕☕ (3) coffees

Thank you for using your stats and programming gifts in such a useful, generous manner. -Jess

Mateu Servera bought ☕☕☕ (3) coffees

A job that must have cost far more coffees than we can afford you ;-). Thank you.

@cdrawn bought ☕☕☕ (3) coffees

Thank you! Such a great resource for teaching these concepts, especially CI, Power, correlation.

Julia bought ☕☕☕ (3) coffees

Fantastic work with the visualizations!

@felixthoemmes bought ☕☕☕ (3) coffees

@dalejbarr bought ☕☕☕ (3) coffees

Your work is amazing! I use your visualizations often in my teaching. Thank you. 

@PsychoMouse bought ☕☕☕ (3) coffees

Excellent!  Well done!  SOOOO Useful!😊 🐭 

@whlevine bought ☕☕ (2) coffees

Thank you so much for these amazing visualizations. They're a great teaching tool and the allow me to show students things that it would take me weeks or months to program myself.

Someone bought ☕☕ (2) coffees

@notawful bought ☕☕ (2) coffees

Thank you for sharing your visualization skills with the rest of us! I use them frequently when teaching intro stats. 

@inthelabagain bought ☕ (1) coffee

Really wonderful visuals, and such a fantastic and effective teaching tool. So many thanks!

Someone bought ☕ (1) coffee

Someone bought ☕ (1) coffee

Yashashree Panda bought ☕ (1) coffee

I really like your work.

Ben bought ☕ (1) coffee

You're awesome. I have students in my intro stats class say, "I get it now," after using your tool. Thanks for making my job easier.

Gabriel Recchia bought ☕ (1) coffee

Incredibly useful tool!

Shiseida Sade Kelly Aponte bought ☕ (1) coffee

Thanks for the assistance for RSCH 8210.

@Benedikt_Hell bought ☕ (1) coffee

Great tools! Thank you very much!

Amalia Alvarez bought ☕ (1) coffee

@noelnguyen16 bought ☕ (1) coffee

Hi Kristoffer, many thanks for making all this great stuff available to the community!

Eran Barzilai bought ☕ (1) coffee

These visualizations are awesome! thank you for creating it

Someone bought ☕ (1) coffee

Chris SG bought ☕ (1) coffee

Very nice.

Gray Church bought ☕ (1) coffee

Thank you for the visualizations. They are fun and informative.

Qamar bought ☕ (1) coffee

Tanya McGhee bought ☕ (1) coffee

@schultemi bought ☕ (1) coffee

Neilo bought ☕ (1) coffee

Really helpful visualisations, thanks!

Someone bought ☕ (1) coffee

This is amazing stuff. Very slick. 

Someone bought ☕ (1) coffee

Sarko bought ☕ (1) coffee

Thanks so much for creating this! Really helpful for being able to explain effect size to a clinician I'm doing an analysis for. 

@DominikaSlus bought ☕ (1) coffee

Thank you! This page is super useful. I'll spread the word. 

Someone bought ☕ (1) coffee

Melinda Rice bought ☕ (1) coffee

Thank you so much for creating these tools! As we face the challenge of teaching statistical concepts online, this is an invaluable resource.

@tmoldwin bought ☕ (1) coffee

Fantastic resource. I think you would be well served to have one page indexing all your visualizations, that would make it more accessible for sharing as a common resource.

Someone bought ☕ (1) coffee

Fantastic Visualizations! Amazing way to to demonstrate how n/power/beta/alpha/effect size are all interrelated - especially for visual learners! Thank you for creating this?

@jackferd bought ☕ (1) coffee

Incredible visualizations and the best power analysis software on R.

Cameron Proctor bought ☕ (1) coffee

Great website!

Someone bought ☕ (1) coffee

Hanah Chapman bought ☕ (1) coffee

Thank you for this work!!

Someone bought ☕ (1) coffee

Jayme bought ☕ (1) coffee

Nice explanation and visual guide of Cohen's d

Bart Comly Boyce bought ☕ (1) coffee

thank you

Dr. Mitchell Earleywine bought ☕ (1) coffee

This site is superb!

Florent bought ☕ (1) coffee

Zampeta bought ☕ (1) coffee

thank you for sharing your work. 

Mila bought ☕ (1) coffee

Thank you for the website, made me smile AND smarter :O enjoy your coffee! :)

Deb bought ☕ (1) coffee

Struggling with statistics and your interactive diagram made me smile to see that someone cares enough about us strugglers to make a visual to help us out!😍 

Someone bought ☕ (1) coffee

@exerpsysing bought ☕ (1) coffee

Much thanks! Visualizations are key to my learning style! 

Someone bought ☕ (1) coffee


You can sponsor my open source work using GitHub Sponsors and have your name shown here.

Backers ✨❤️

Questions & Comments

Please use GitHub Discussions for any questions related to this post, or open an issue on GitHub if you've found a bug or wan't to make a feature request.


There are no webmentions for this page

(Webmentions sent before 2021 will unfortunately not show up here.)

Archived Comments (13)

Hedvig Skirgård 2018-12-14

Tack, det här är jättebra. Jag måste dock tweaka lite pga vill inte ha default centrering, så det blir lite krångliare. Jag har hellre en brytning i atlanten än i Stilla Havet, så dom flesta lösningar jag hittar för bra projektioner måste jag modda mer.. men.. jaja. Tack i alla fall ^^!

JPMD 2016-08-13

Great Tutorial. Thanks Kristoffer. I do have a problem with the projection when I get the map data from maps library:
map <- map_data("world")
map <- subset(map, region!="Antarctica")
map <- spTransform(map, CRS("+proj=wintri"))

Gives me this error:

Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘spTransform’ for signature ‘"data.frame", "CRS"’

Any clue of why this might be?

Sri Satya 2016-02-23

could you guys please help me out with my query.............I would like to create a calender using (R- studio) and i want to show the description pop-up on the date field when i moves my courser on a
specified date.

mielniczuk 2015-02-25

wanting to learn how to use shape files within R, tried replicating your tutorial. Using latest ver of required libraries and shape files. Now getting err: - Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file. Online references point to 'possible errs in topography'. I notice that your tutorial on R-bloggers now displays the map graphics as broken links. Any thoughts?

Sabrina 2014-02-20

I tried but didn't succeed (sorry, just started with R), how may I change the colours of the countries, e.g. fill the United States with blue and Japan with yellow? Is there an easy way to do that?

Sabrina 2014-02-19

I think readOGR/rgdal changed somehow (and I have the latest version), it only worked when I read it in as wmap <- readOGR(dsn="path to the folder which contains the shapefiles", layer="ne_110m_land").

Amazing tutorial!!

Friso Muijsers 2013-11-27

Short update: I managed to project my own data onto the map, however I failed in converting my long-lat data to robinson projection. Have to use the normal projection instead.

Friso Muijsers 2013-11-22

Hi, great post!! Is there a simple way to create a custom file with places instead of the one that you used. I have data from different places with long-and latt data, which I would love to project on that map.



irakliloladze 2013-11-02

Thank you for the great and elegant post! Is there a simple way to add a specific latitude to the map? grat gives only a choice of major latitudes. What if I only want to draw a single latitude (and no longitudes) on the map?

Matt 2013-10-17

Thanks for this. Nice post. Pretty new to the idea of mapping in R, surprised that there were so few tutorials around on the web. I was working on pulling the country names in too so you could tag a lists of countries for coding appropriately etc. Will look to post that back here once I have it refined. Cheers

Jeremy 2013-08-24

This is great. Very easy to follow and expand upon. Actually, I was trying to grab the ocean shapefiles as well (I know it seems strange to overlay the ocean shapes onto the land shapefiles, but for I am doing it makes sense) and the website says that the "polygons are split into contiguous pieces." When I plot this downloaded files, it seems that it only shows the oceans around Asia and Africa. Not sure if that is supposed to be that way or I have something wrong. In the latter instance, using the routine described above does anyone know of a way to join these pieces together?

Michael MacAskill 2013-05-27

Nice post. I know the Robinson and Winkel projections work better overall, but being a Kiwi I prefer the old fashioned Mercator projection if only because the others tend to turn New Zealand into a disortorted smear at the edge of the world...

Kristoffer Magnusson 2013-05-27

Well, the Mercator projection will make Sweden look bigger than it is, so I'm not wholly against it. Luckily Sweden is close to the prime median, else it would probably look like New Zealand...