In this post, I will present a way to plot a European Union map using R. We ran across this question with @gVermandel.
EDIT (2013-02-18) : As mentioned in the comment section below, there is a nice package I wasn’t aware of, that does (with more ease and offering a more beautiful result) the same thing: https://github.com/briatte/kmaps.
As you know, some countries are missing in the maps package. So, instead of working with that package, we will use the rworldmap one, available on CRAN. We will also need two more packages : ggplot2 and grid.
library(ggplot2)
library(grid)
library(rworldmap)
First, let’s get the coordinates of every countries from the getMap function. We need the names of the 28 member states of the E.U..
# Get the world map
worldMap <- getMap()
# Member States of the European Union
europeanUnion <- c("Austria","Belgium","Bulgaria","Croatia","Cyprus",
"Czech Rep.","Denmark","Estonia","Finland","France",
"Germany","Greece","Hungary","Ireland","Italy","Latvia",
"Lithuania","Luxembourg","Malta","Netherlands","Poland",
"Portugal","Romania","Slovakia","Slovenia","Spain",
"Sweden","United Kingdom")
# Select only the index of states member of the E.U.
indEU <- which(worldMap$NAME%in%europeanUnion)
Then we can get the coordinates for each country.
# Extract longitude and latitude border's coordinates of members states of E.U.
europeCoords <- lapply(indEU, function(i){
df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
df$region =as.character(worldMap$NAME[i])
colnames(df) <- list("long", "lat", "region")
return(df)
})
europeCoords <- do.call("rbind", europeCoords)
For the purpose of this example, we’ll assign some random values to each country.
# Add some data for each member
value <- sample(x = seq(0,3,by = 0.1), size = length(europeanUnion),
replace = TRUE)
europeanUnionTable <- data.frame(country = europeanUnion, value = value)
europeCoords$value <- europeanUnionTable$value[match(europeCoords$region,europeanUnionTable$country)]
Finally, we can plot the map.
# Plot the map
P <- ggplot() + geom_polygon(data = europeCoords, aes(x = long, y = lat, group = region, fill = value),
colour = "black", size = 0.1) +
coord_map(xlim = c(-13, 35), ylim = c(32, 71))
P <- P + scale_fill_gradient(name = "Growth Rate", low = "#FF0000FF", high = "#FFFF00FF", na.value = "grey50")
P <- P + theme(#panel.grid.minor = element_line(colour = NA), panel.grid.minor = element_line(colour = NA),
#panel.background = element_rect(fill = NA, colour = NA),
axis.text.x = element_blank(),
axis.text.y = element_blank(), axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(), axis.title = element_blank(),
#rect = element_blank(),
plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))

Member Countries of the European Union (Warning: fake data)
The legend title is a bit too close to the legend keys to my opinion… As suggested by Ronald Burggraaf (thank you!), a way to add some space between these elements is by adding a breakline character to the title :
P <- P + scale_fill_gradient(name = "Growth Raten", low = "#FF0000FF", high = "#FFFF00FF", na.value = "grey50")
Here’s a method to do the same thing with ESRI shapefiles: https://github.com/briatte/kmaps
Oh why, thank you! I’ll edit my post to mention your package!
Great stuff, thanks.
Are you aware of any R package that includes historic European boundaries, hopefully going back to the medieval period?
Sorry for the long delay and the answer: I do not. Maybe you can work with these data: https://github.com/kgjenkins/tq-world-historical-boundaries.
Hi, thank you so much for this!
have a question. Do you know by any chance how to add migration flows on this map. lets say I have data how many people went to each country is it a way to code that into the map. like to make circles where there are the most migrants?
Thank you
Hi there! Sorry for the delay. If you want circles, the simplest solution that comes to my is to add a layer with `geom_point()` where you map the size of the points to your variable giving the number of migrants. Then you can adjust the size of the points with a scale function (https://ggplot2.tidyverse.org/reference/scale_size.html).
Hello
I have a Error when I try. It doesn’t work
“Error in loadNamespace(name) : there is no package called ‘mapproj’”
Hi, you need to install a missing package: `mapproj` (and also, if I recall correctly, some missing libraries outside R).
Hi everyone
it does not work. I don’t know why but it doesn’t. Could be my R-Studio?