basemap_dem
adds a digital elevation model (DEM) raster map of China as a layer to ggplot2.
The function ensures the output map remains rectangular, regardless of the chosen projection.
It supports displaying the DEM either within China's boundary or in a larger rectangular area
around China. Users can provide their own DEM data using the data
parameter, or the default
built-in DEM data will be used.
Usage
basemap_dem(
data = NULL,
crs = NULL,
within_china = FALSE,
maxcell = 1e+06,
na.rm = FALSE,
...
)
Arguments
- data
Optional. A
terra
raster object for custom DEM data. IfNULL
(default), the function uses the built-in DEM data (gebco_2024.tif
).- crs
Coordinate reference system (CRS) for the projection. Defaults to the CRS of the DEM data. Users can specify other CRS strings (e.g.,
"EPSG:4326"
or custom projections).- within_china
Logical. If
TRUE
, displays only the DEM within China's boundary. IfFALSE
, displays the DEM for a larger rectangular area around China. Default isFALSE
.- maxcell
Maximum number of cells for rendering (to improve performance). Defaults to
1e6
.- na.rm
Logical. If
TRUE
, removes missing values. Default isFALSE
.- ...
Additional parameters passed to
geom_spatraster
.
Examples
# Define Azimuthal Equidistant projection centered on China
china_proj <- "+proj=aeqd +lat_0=35 +lon_0=105 +ellps=WGS84 +units=m +no_defs"
# Example 1: Display full rectangular area around China using built-in DEM data
ggplot() +
basemap_dem(within_china = FALSE) +
tidyterra::scale_fill_hypso_tint_c(
palette = "gmt_globe",
breaks = c(-10000, -5000, 0, 2000, 5000, 8000)
) +
theme_minimal()
#> <SpatRaster> resampled to 1001520 cells.
# Example 2: Display only China's DEM and boundaries using built-in DEM data
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()
#> Warning: [vect] returning polygons ignoring additional geometry types. Use 'svc' to get all geometries
#> <SpatRaster> resampled to 1000968 cells.
# Example 3: Use custom DEM data by specifying the path to your DEM file
# dem_path <- "path/to/your/dem_file.tif" # Specify the path to your DEM file
dem_path <- system.file("extdata", "gebco_2024.tif", package = "ggmapcn") # Use the built-in DEM data
ggplot() +
basemap_dem(data = terra::rast(dem_path), within_china = FALSE) +
theme_minimal()
#> <SpatRaster> resampled to 1001520 cells.