Plot a world map with a specified grid with R

Learn plot a world map with a specified grid with r with practical examples, diagrams, and best practices. Covers r, latitude-longitude, r-maptools development techniques with visual explanations.

Plotting a World Map with a Custom Grid in R

Hero image for Plot a world map with a specified grid with R

Learn how to visualize global data by plotting a world map in R and overlaying a custom grid based on latitude and longitude coordinates.

Visualizing geographical data on a world map is a common task in many fields, from environmental science to social studies. R provides powerful tools for geospatial analysis and visualization. This article will guide you through the process of plotting a world map and, more importantly, overlaying a custom grid based on specified latitude and longitude intervals. This technique is particularly useful for analyzing data aggregated by geographical cells or for creating custom reference systems on a global scale.

Setting Up Your R Environment

Before we begin plotting, ensure you have the necessary R packages installed. We'll primarily use rnaturalearth for world map data, ggplot2 for plotting, and sf for spatial data manipulation. If you don't have them, you can install them using the install.packages() function.

install.packages(c("rnaturalearth", "ggplot2", "sf"))
library(rnaturalearth)
library(ggplot2)
library(sf)

Installing and loading required R packages.

Retrieving World Map Data

The rnaturalearth package provides convenient access to vector map data from Natural Earth. We'll fetch the world map at a 1:110 million scale, which is suitable for global visualizations. This data will be returned as an sf object, making it easy to work with in ggplot2.

world_map <- ne_countries(scale = "small", returnclass = "sf")

Fetching world map data using rnaturalearth.

Defining and Generating the Custom Grid

The core of this task is creating a custom grid. We'll define the latitude and longitude intervals for our grid lines. Then, we'll use expand.grid() to generate all combinations of these coordinates, forming the intersections of our grid. These points will then be converted into an sf object to ensure compatibility with our map data and ggplot2's spatial capabilities.

flowchart TD
    A[Define Lat/Lon Intervals] --> B{Generate Grid Points}
    B --> C[Convert Points to SF Object]
    C --> D[Create Grid Lines from Points]
    D --> E[Overlay Grid on Map]

Workflow for generating and overlaying a custom grid on a map.

# Define grid intervals
lon_breaks <- seq(-180, 180, by = 30) # Every 30 degrees longitude
lat_breaks <- seq(-90, 90, by = 15)   # Every 15 degrees latitude

# Create grid lines
grid_lines_lon <- data.frame(lon = lon_breaks, lat_start = -90, lat_end = 90)
grid_lines_lat <- data.frame(lat = lat_breaks, lon_start = -180, lon_end = 180)

# Convert to sf objects for plotting
grid_sf_lon <- grid_lines_lon %>%
  st_as_sf(coords = c("lon", "lat_start"), crs = 4326) %>%
  st_segment(st_point(c(lon, lat_end)), by = "lon") # This part needs careful handling for lines

# A more robust way to create grid lines as sf objects:
# Create longitude lines
lon_grid_sf <- lapply(lon_breaks, function(lon_val) {
  st_sfc(st_linestring(matrix(c(lon_val, -90, lon_val, 90), ncol = 2, byrow = TRUE)), crs = 4326)
}) %>%
  do.call(c, .)

# Create latitude lines
lat_grid_sf <- lapply(lat_breaks, function(lat_val) {
  st_sfc(st_linestring(matrix(c(-180, lat_val, 180, lat_val), ncol = 2, byrow = TRUE)), crs = 4326)
}) %>%
  do.call(c, .)

# Combine into a single sf object for convenience
full_grid_sf <- c(lon_grid_sf, lat_grid_sf)

Defining latitude and longitude breaks and generating sf line objects for the grid.

Plotting the Map with the Grid Overlay

Now, we'll use ggplot2 to combine our world map and the custom grid. We'll use geom_sf() to plot both the country polygons and the grid lines. We can customize colors, line types, and add a title for better presentation. The coord_sf() function is crucial for handling spatial projections correctly.

ggplot() +
  geom_sf(data = world_map, fill = "lightgray", color = "darkgray") +
  geom_sf(data = full_grid_sf, color = "blue", linetype = "dotted", size = 0.3) +
  coord_sf(xlim = c(-180, 180), ylim = c(-90, 90), expand = FALSE) +
  labs(title = "World Map with Custom Lat/Lon Grid",
       x = "Longitude", y = "Latitude") +
  theme_minimal()

Plotting the world map with the custom grid using ggplot2.