geom_world: Plot World Map with Customizable Options

View source: R/geom_world.R

geom_worldR Documentation

Plot World Map with Customizable Options

Description

A wrapper around [ggplot2::geom_sf()] for visualizing world maps with customizable options. This function allows for custom projections, filtering specific countries or regions, and detailed aesthetic customizations for borders and fills.

Usage

geom_world(
  data = NULL,
  crs = "+proj=longlat +datum=WGS84",
  color = "black",
  fill = "white",
  linewidth = 0.5,
  filter_attribute = "SOC",
  filter = NULL,
  ...
)

Arguments

data

An 'sf' object containing world map data. If 'NULL', the default world map data from the package will be loaded from a '.rda' file.

crs

A character string specifying the target coordinate reference system (CRS) for the map projection. Defaults to '"+proj=longlat +datum=WGS84"'.

color

A character string specifying the border color for administrative boundaries. Default is '"black"'.

fill

A character string specifying the fill color for administrative areas. Default is '"white"'.

linewidth

A numeric value specifying the line width for administrative boundaries. Default is '0.5'.

filter_attribute

A character string specifying the column name used for filtering countries or regions. Default is '"SOC"', which refers to the ISO 3166-1 alpha-3 country code in the default dataset.

filter

A character vector specifying the values to filter specific countries or regions. Default is 'NULL'.

...

Additional parameters passed to [ggplot2::geom_sf()], such as 'size', 'alpha', or 'lty'.

Details

This function simplifies the process of creating world maps by combining the functionality of 'geom_sf' with user-friendly options for projections, filtering, and custom styling. Key features include: - **Custom projections**: Easily apply any CRS to the map. - **Filtering by attributes**: Quickly focus on specific countries or regions. - **Flexible aesthetics**: Customize fill, borders, transparency, and other visual properties.

Value

A 'ggplot2' layer for world map visualization.

See Also

[ggplot2::geom_sf()], [sf::st_transform()], [sf::st_read()]

Examples


  # Plot the default world map
  ggplot() +
    geom_world() +
    theme_minimal()

  # Using Robinson projection with central meridian at 0°
  ggplot() +
    geom_world(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m") +
    theme_minimal()

  # Filter specific countries (e.g., China and its neighbors)
  china_neighbors <- c("CHN", "AFG", "BTN", "MMR", "LAO", "NPL", "PRK", "KOR",
                       "KAZ", "KGZ", "MNG", "IND", "BGD", "TJK", "PAK", "LKA", "VNM")
  ggplot() +
    geom_world(filter = china_neighbors) +
    theme_minimal()

  # Background map + Highlight specific region
  ggplot() +
    geom_world(fill = "gray80", color = "gray50", alpha = 0.5) +
    geom_world(filter = c("CHN"), fill = "red", color = "black", linewidth = 1.5) +
    theme_minimal()

  # Customize styles with transparency and bold borders
  ggplot() +
    geom_world(fill = "lightblue", color = "darkblue", linewidth = 1, alpha = 0.8) +
    theme_void()


ggmapcn documentation built on April 12, 2025, 2:09 a.m.