This post is a brief follow-up to a question that appeared some time ago on the “The R Project for Statistical Computing” LinkedIn group, which I’m reporting here:
How can I draw a map of MODERN Europe?
Hi, I'm trying to draw a map of modern Europe but I've found only maps of twenty years ago, with Yugoslavia and Czechoslovakia still united!!!
Does anyone know where I can get a more recent map to be employed with packages such as 'sp' or 'maps'?
Thank you very much!
Two different solutions to the above question will be provided here, using two different R packages.
Solution #1 - ggmap
The package ggmap allows visualizations of spatial data on maps retrieved from Google Maps, OpenStreetMap or other services.
A map of Europe is obtained with just four lines of R code (including the loading of packages.)
> library(ggmap) > library(mapproj) > map <- get_map(location = 'Europe', zoom = 4) > ggmap(map) |
The second line gets the map from Google Maps, unless a different source parameter is specified (for example source = "osm" for retrieving the map from OpenStreetMap.)
The argument location requires the text you would type into Google Maps search box if you were looking to obtain your map online.
The zoom argument requires an integer. Zero would give a map of the whole world, while the max value of 21 returns a map to the building detail. I chose a value of 4 to display Europe after some trial and error process.
The final line is the one actually drawing the map.
Note that the object returned by ggmap is of class ggplot, thus anything you would normally do with a ggplot (like adding geometries) can be done with ggmap.
Solution #2 – rworldmap
The map produced by the ggmap package is a raster. In other words it is just an image placed on the R graphic device. While it serves the purpose of displaying Europe, and would be fine for the display of spatial point patterns, the raster map is not very convenient when one has data aggregated at the country level and wants to show them by color-coding each country.
Package rworldmap is better suited for the task, as it provides maps as spatial polygons.
Again, plotting a map is just a matter of three lines of code, package loading included.
> library(rworldmap) > newmap <- getMap(resolution = "low") > plot(newmap) |
The code is very similar to the one of ggmap. The resolution argument is quite self-explanatory and you can see from the resulting map that "low" is actually a more than acceptable resolution.
In this case we got a map of the whole world. By simply tinkering with the xlim and ylim arguments of the plot function we can limit the display to just Europe.
> plot(newmap, > xlim = c(-20, 59), > ylim = c(35, 71), > asp = 1 > ) |
Admittedly I got the right numbers for the xlim and ylim parameters with some trial and error process this time too.
Would it be possible to retrieve them in a more automated way?
A quick look at geocoding
Wikipedia has the this entry on the extreme points of Europe.
The geocode function from the ggmap package finds the coordinates of a location using Google Maps. Thus, finding the coordinates of the European most extreme points is as easy as typing the following code:
> library(ggmap)
> europe.limits <- geocode(c("CapeFligely,RudolfIsland,Franz Josef Land,Russia",
> "Gavdos,Greece",
> "Faja Grande,Azores",
> "SevernyIsland,Novaya Zemlya,Russia")
> )
> europe.limits
lon lat
1 54.78333 80.56667
2 24.08464 34.83469
3 -31.26192 39.45479
4 59.34569 62.21215 |
The xlim and ylim arguments passed to the plot function in the previous section can slightly be modified like this:
> plot(newmap, > xlim = range(europe.limits$lon), > ylim = range(europe.limits$lat), > asp = 1 > ) |
What’s next?
As R users we hardly need a map that does not feature any data, thus in future posts we will have a look at how to visualize both spatial point patterns and spatially aggregated data on maps.
We will also provide sources to retrieve spatial polygons for different levels of geographical entities, such as regions for example.
View (and download) the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | library(ggmap)
library(mapproj)
map <- get_map(location = 'Europe', zoom = 4)
ggmap(map)
library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap)
plot(newmap,
xlim = c(-20, 59),
ylim = c(35, 71),
asp = 1
)
library(ggmap)
europe.limits <- geocode(c("CapeFligely,RudolfIsland,Franz Josef Land,Russia",
"Gavdos,Greece",
"Faja Grande,Azores",
"SevernyIsland,Novaya Zemlya,Russia")
)
europe.limits
plot(newmap,
xlim = range(europe.limits$lon),
ylim = range(europe.limits$lat),
asp = 1
) |




I can see this as being very useful, but couldn't get ggmap examples to work. I couldn't find either get_map or geocode functions anywhere?
What kind of error do you get when you type:
library(ggmap)
map <- get_map()
You installed packages ggmap, mapproj and all required dependencies, right? So, please copy-paste the error you get.
Nicola
Hey Max!
Would be super awesome if you could cover maps and mapping for the different NUTS levels! I know you are conversant in this realm as you seem to work for government.
And please check out my article on a mysterious increase of deaths caused by viral hepatitis in Germany 1998. There is a chance that you find this increase also in Italy. I'd like to hear your opinion about it!
Cheers
Hi Max Marchi. I have the same problem as griffilo. I've installed all the required packages and I got this error in solution #1:
Error: could not find function "get_map"
Can you help me?
TY
I suggest everyone who is having griffilo's (and Mika4real's) issues, to type
sessionInfo()in the R console and copy/paste the resulting information here.Hopefully we'll be able to help out.
And even better, and almost just as easy, is to use the vector data from Natural Earth. These are free (license: public domain), detailed, available in various scales, of very high quality and up-to-date. Use the
readOGR()function in thergdalpackage to load the map data into R.One very important thing to keep in mind when plotting maps is that you have to use an appropriate projection (and don’t fiddle with the aspect ratio manually). For Europe, use either the equal-area ETRS-LAEA (EPSG 3035) projection or the conformal ETRS-LCC (EPSG 3034) projection. It’s extremely easy to reproject map data in R. Example:
mymap.unpro=readOGR(…) # Read in (unprojected) map data
pro=CRS("+init=epsg:3035") # The projection to use
mymap.pro=spTransform(bb.pro, pro) # Reproject the map
For more about the two projections mentioned above, see the EU report Map Projections for Europe.
Thank you for the post.
Would you please explain how can I overlap a plot (with color) on the map? I would like to overlay Cincinnati's crime data on the map. The data is structured into cells of of width 0.002x0.002 (lon,lat) and each cell contains number of crimes happened in that place. How can I show it as a heat map (or something similar) while the underlying can still be seen?
Thanks
Next post has examples of plotting data over the map (including transparency options to have the underlying still visible.)
Hope that one will answer your question, otherwise I'll be happy to provide more help/links.
Thank you for your reply.
I was wondering if you are going to post your second post about maps here in this blog or somewhere else.
Thanks
It will be here, I believe in a couple of days.
Hi Max. Thank you for the post.
I was wondering if it is important to add scale bar or compass rose to the map, and if so, if there is any way to do it with the ggmap package.
Thank you
Pingback: Maps in R: Plotting data points on a map « Another Word For It