Adding_Spatial_Data

library(ggmapcn)
# Define Azimuthal Equidistant projection centered on China
china_proj <- "+proj=aeqd +lat_0=35 +lon_0=105 +ellps=WGS84 +units=m +no_defs"

Introduction

This vignette introduces two functions in the ggmapcn package: geom_loc and geom_vege_raster. These functions extend the capabilities of the package for spatial data visualization.

geom_loc: Adding Spatial Point Data Layer with Color by Grouping

The geom_loc function allows you to add spatial point data to a ggplot, supporting both sf and tabular data frames, with color mapping based on a grouping variable.

# Create a ggplot with spatial points colored by 'Category'
set.seed(123)
data_sim <- data.frame(
   Longitude = runif(100, 80, 120),
   Latitude = runif(100, 28, 40),
   Category = sample(c("Type A", "Type B", "Type C"), 100, replace = TRUE)
   )
ggplot() +
   geom_boundary_cn() +
   geom_loc(
     data = data_sim, lon = "Longitude", lat = "Latitude",
     mapping = aes(color = Category), size = 1, alpha = 0.7
   ) +
   theme_bw()

Sample site

basemap_vege: Vegetation Map of China Layer for ggplot2

Before using the function, ensure that the necessary data files are available by running:

# This function checks if the required data files are available
# It may take some time, especially if your network connection is slow.
  check_geodata(files = c("vege_1km_projected.tif"), quiet = FALSE)

The basemap_vege function adds a vegetation raster map of China with color-coded vegetation types to a ggplot.

# Add vegetation raster of China to a ggplot
ggplot() +
  basemap_vege() +
  guides(fill = guide_none()) +
  theme_bw()

basemap_dem: Elevation Map of China Layer for ggplot2

The basemap_dem function adds a digital elevation model (DEM) raster map of China as a layer to ggplot2.

# Apply Azimuthal Equidistant projection centered on China
ggplot() +
  basemap_dem(crs = china_proj, within_china = TRUE) +
  geom_boundary_cn(crs = china_proj) +
  tidyterra::scale_fill_hypso_c(
    palette = "dem_print",
    breaks = c(0, 2000, 4000, 6000),
    limits = c(0, 7000)
  ) +
  labs(fill = "Elevation (m)") +
  theme_minimal() +
  theme(legend.position = "bottom")
#> Warning in value[[3L]](cond): Attempt 1 failed to download gebco_2024_China.tif
#> due to error: Transferred a partial file [raw.githubusercontent.com]: end of
#> response with 22182632 bytes missing
#> Retrying in 5 seconds...
#> Warning in value[[3L]](cond): Attempt 2 failed to download gebco_2024_China.tif
#> due to error: Transferred a partial file [raw.githubusercontent.com]: end of
#> response with 23348180 bytes missing
#> Retrying in 5 seconds...
#> Warning in value[[3L]](cond): Failed to download gebco_2024_China.tif after 3
#> attempts.
#> <SpatRaster> resampled to 1000968 cells.

Elevation Map of China

coord_proj: Transforming Limits for Custom Projections

The coord_proj function is a wrapper around coord_sf that allows you to specify map limits (xlim, ylim) in longitude and latitude (WGS84 CRS) and automatically transforms them into the specified CRS for accurate projections.

Here, the Azimuthal Equidistant projection centered on China is applied, with transformed map limits specified in longitude and latitude.

# World map with Azimuthal Equidistant projection centered on China
ggplot() +
  geom_world(fill = "lightblue") +
  coord_proj(
    crs = china_proj,
    xlim = c(60, 140),
    ylim = c(10, 50)
  ) +
  theme_minimal()

clip region

Additionally, we can focus the map on the South China Sea Islands. The longitude and latitude range shown here is for demonstration purposes only.

ggplot() +
  geom_boundary_cn() +
  theme_bw() +
  coord_proj(
    crs = china_proj,
    expand = FALSE,
    xlim = c(105, 126),
    ylim = c(2, 23)
  )

Naihai