Mapping in R

Create a map of Europe in ggplot2.

Miriam Lerma true
2023-03-04

Intro

How to create a map in ggplot and add attributes.

1. Getting ready

You can download shapefiles from: https://www.naturalearthdata.com/downloads/

Call the package here to work in your directory.

Call the package sf to read the shapefiles into R

2. Read your shapefile

Use your directory name, and give the name of your shapefile

Europe<-st_read(paste0(Directory,MyShapefileName))
Reading layer `country' from data source 
  `C:\Users\lerma\OneDrive\Documents\4Programacion\2023\1Distill\Distill\_posts\2023-03-04-mapping-in-r\country.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 54 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -31.26575 ymin: 32.39748 xmax: 69.07032 ymax: 81.85737
Geodetic CRS:  WGS 84

3. Create a basic map

Load ggplot2

Plot your shapefile

ggplot()+  
  geom_sf(data = Europe)

4. Add colors

I copy the hex colors from coolors

ggplot()+  
  geom_sf(data = Europe, 
          colour = "#edf2f4", 
          fill = "#2b2d42",
          size=0.5)

5. Change background color

You can eliminate the grids and the change the background color in ggplot

ggplot()+  
  geom_sf(data = Europe, 
          colour = "#edf2f4", 
          fill = "#2b2d42",
          size=0.5)+
  theme(
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = '#edf2f4'),
        legend.background = element_rect(fill = '#edf2f4'))

6. Add limits

Reduce to focus in your area of interest

ggplot()+  
  geom_sf(data = Europe, 
          colour = "#edf2f4", 
          fill = "#2b2d42",
          size=0.5)+
  theme(
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = '#edf2f4'),
        legend.background = element_rect(fill = '#edf2f4'))+
  
  coord_sf(xlim = c(9, 31),ylim = c(53, 65))

7. Add scale

Load the package ggspatial to add a scale and an north arrow

br is from bottom right
bar_cols is for column colors
text_col is for the color of the text

ggplot()+  
  geom_sf(data = Europe, 
          colour = "#edf2f4", 
          fill = "#2b2d42",
          size=0.5)+
  theme(
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = '#edf2f4'),
        legend.background = element_rect(fill = '#edf2f4'))+
  
  coord_sf(xlim = c(9, 31),ylim = c(53, 65))+
    scale_x_continuous(labels = function(x) paste0(x, '\u00B0', "W")) +
    scale_y_continuous(labels = function(x) paste0(x, '\u00B0', "N")) +
  
  annotation_scale(location = "br",bar_cols = c("#ef233c", "#d90429"),text_col = '#ef233c')

8. Add arrow

tl is for top left
which_north preferably true (see why here)
north_arrow_fancy_orienteering (see other styles here)

ggplot()+  
  geom_sf(data = Europe, 
          colour = "#edf2f4", 
          fill = "#2b2d42",
          size=0.5)+
  theme(
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = '#edf2f4'),
        legend.background = element_rect(fill = '#edf2f4'))+
  
  coord_sf(xlim = c(9, 31),ylim = c(53, 65))+
    scale_x_continuous(labels = function(x) paste0(x, '\u00B0', "W")) +
    scale_y_continuous(labels = function(x) paste0(x, '\u00B0', "N")) +
  
   annotation_scale(location = "br",bar_cols = c("#ef233c", "#d90429"),text_col = '#ef233c')+
  
  annotation_north_arrow(location = "tl", style = north_arrow_minimal(text_col = '#ffd60a',line_col = '#ffd60a',fill = '#ffd60a')) 

Thats it for now!