R语言极射赤平投影地图添加坐标格网求助(支持base R/ggplot2)
Got it! Let's figure out how to add coordinate graticule lines to your stereographic projection map—we'll cover both base R and ggplot2 approaches since you mentioned either works. Here are step-by-step solutions with code you can adapt right away.
Base R Implementation
Base R works great for this if you prefer a more traditional workflow, using the sf package for modern, reliable spatial data handling and projections.
Step-by-Step Code
# Load required packages library(sf) library(maps) # 1. Generate 10-degree interval graticule lines (adjust step size as needed) lat_intervals <- seq(-90, 90, 10) lon_intervals <- seq(-180, 180, 10) # Create a grid of all lat/lon combinations for the graticule graticule_points <- expand.grid(lon = lon_intervals, lat = lat_intervals) # Convert to spatial object with WGS84 (EPSG:4326) coordinate system graticule_sf <- st_as_sf(graticule_points, coords = c("lon", "lat"), crs = 4326) # 2. Define your stereographic projection (this example is South Pole-centered) # Adjust `lat_0` to 90 for North Pole, or change `lon_0` for a different central meridian stere_proj <- "+proj=stere +lat_0=-90 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs" # Transform graticule and world map to stereographic projection graticule_stere <- st_transform(graticule_sf, crs = stere_proj) world_sf <- st_as_sf(map("world", plot = FALSE, fill = TRUE)) world_stere <- st_transform(world_sf, crs = stere_proj) # 3. Plot the map and graticule plot(st_geometry(world_stere), col = "lightgray", border = "dimgray") # Add meridians (longitude lines) for (lon in lon_intervals) { meridian <- graticule_stere[graticule_points$lon == lon, ] plot(st_geometry(meridian), add = TRUE, col = "darkgray", lty = 2) } # Add parallels (latitude lines) for (lat in lat_intervals) { parallel <- graticule_stere[graticule_points$lat == lat, ] plot(st_geometry(parallel), add = TRUE, col = "darkgray", lty = 2) } title("Stereographic Projection with Graticule (Base R)")
Key Notes
- Adjust the
seq()step sizes (e.g.,5instead of10) for a denser graticule. - Modify the
stere_projstring to center on the North Pole (lat_0=90) or a different central longitude.
ggplot2 Implementation
If you prefer the flexibility and aesthetics of ggplot2, this approach uses sf alongside ggplot's spatial geom functions for a cleaner, more customizable plot.
Step-by-Step Code
# Load required packages library(ggplot2) library(sf) library(maps) # 1. Prepare world map data in stereographic projection world_sf <- st_as_sf(map("world", plot = FALSE, fill = TRUE)) stere_proj <- "+proj=stere +lat_0=-90 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs" world_stere <- st_transform(world_sf, crs = stere_proj) # 2. Generate and transform graticule lines # `st_graticule()` creates a ready-to-use graticule spatial object graticule <- st_graticule(lat = seq(-90, 90, 10), lon = seq(-180, 180, 10)) graticule_stere <- st_transform(graticule, crs = stere_proj) # 3. Build the ggplot ggplot() + # Base world map geom_sf(data = world_stere, fill = "lightgray", color = "dimgray") + # Custom graticule lines geom_sf(data = graticule_stere, color = "darkgray", linetype = "dashed") + # Clean up theme (turn off default grid to avoid overlap) theme_minimal() + theme(panel.grid = element_blank()) + labs(title = "Stereographic Projection with Graticule (ggplot2)")
Key Notes
- Use
geom_sf_text()if you want to add latitude/longitude labels—just filter the graticule points to label key intervals (e.g., every 30 degrees) to avoid clutter. - Tweak colors, line types, and theme elements to match your example map's style.
内容的提问来源于stack exchange,提问作者Philippe Massicotte
相关产品推荐
相关产品推荐

