Plot multiple shapefiles in Rstudio using ggplot2

Plot multiple shapefiles in Rstudio using ggplot2

Introduction

In this course, you will learn how to Plot Multiple Shapefiles (polygons) in RStudio using ggplot2. This involves following best practices in data visualization to effectively communicate the results of your research. To create multiple maps using ggplot2 in RStudio, you can use the functions facet_wrap() or facet_grid() to divide your data based on one or more variables. In this example, we will use facet_wrap() to create a faceted set of maps (lake dynamics from 1984 to 1995). You can further customize the appearance of the maps by adding additional layers or modifying the theme within the ggplot() function. The steps we have followed to create and customize facet_wrap maps are listed below. The steps we followed to create and customize facet_wrap maps are listed below.
The use of this article and the video (link below) is highly recommended for mastering the creation of multiple maps in RStudio using ggplot2.

1. Run the packages already installed

library(tidyverse)
library(ggplot2)
library(sf)
library(ggnewscale)
library(ggspatial)

2. Import Vector Layers

Shapefiles (shp) are commonly used to store geographic information, and R offers several packages that allow users to read and manipulate these files.
> Import lake boundary
Boundary <- st_read("D:/RStudio/Multiple_Vector/Boundary/Boundary.shp", stringsAsFactors = FALSE)
> Import vector layers and build data frame (df). Set your working directory
Path_shp <- list.files(path = "D:/RStudio/Multiple_Vector/Shp_Lake", full.names = TRUE, pattern = ".shp$")
Path_shp
shp <- Path_shp %>% map(read_sf)
shp_full <- bind_rows(shp)
shp_full

3. Create and customize facet_wrap maps

ggplot()+
    geom_sf(data = Boundary, aes(color="#000000"),fill=NA, size = 0.3) +
    scale_color_manual(values=c("#000000"),labels=c("Lake boundary"), name="")+
    new_scale_color() +
    geom_sf(data = shp_full, aes(color="#0C18CF"),fill ="#0C18CF",pch = 17)+
    scale_color_manual(values=c("#0C18CF"),labels=c("Water body"), name="")+
    facet_wrap(~ Date, ncol = 4)+
    coord_sf(datum = st_crs(Boundary))+
    scale_x_continuous(breaks = c(533200, 535500)) +
    scale_y_continuous(breaks = c(338300, 339500))+
theme(
    axis.text.x = element_text(size=8, angle=0,hjust=0.5, vjust = 0.3),
    axis.text.y = element_text(size=8, angle=90,hjust=0.5, vjust = 0.3),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.line = element_line(size = 0.5, color="#000000"),
    #Legend
    legend.position = "bottom",
    legend.box = "horizontal",
    legend.key.size = unit(0.3, 'cm'),
    legend.justification = "center",
    legend.background = element_rect(fill=alpha('#ededed', 0)),
    legend.margin = margin(0,0,0,0, unit="cm"),
    #Strip
    strip.text.x =element_text(size = 8.5,color="#000000",face="bold") )+
    annotation_scale(style = "ticks", width_hint = 0.5, pad_x = unit(0.1, "cm"))+
   annotation_north_arrow(location = "tr", which_north = "true", height = unit(0.5, "cm"), width =             unit(0.5, "cm"), style = north_arrow_minimal, pad_y = unit(0.01, "cm"), pad_x = unit(0.1, "cm"))

> Save maps with height resolution

ggsave("C:/Users/AE/Desktop/Aoua22.png", width = 16, height = 22.5, units = "cm", dpi=300)
Watch the video on YouTube
Plot multiple shapefiles in Rstudio using ggplot2

Comments



Font Size
+
16
-
lines height
+
2
-