You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言地图指定经纬度网格位置绘制风玫瑰图?

Answer

Absolutely, this is totally achievable! The trick is combining base R's map plotting with grid graphics (since openair::windRose uses grid under the hood) to overlay wind roses at specific geographic coordinates. Here's a step-by-step solution using your test data:

Step 1: Load Required Packages

Make sure you have these installed and loaded first:

library(maps)
library(openair)
library(grid)

Step 2: Prepare Your Data (Matching Your Test Code)

# Geographic coordinates
lats <- seq(18, 30, 3)
lons <- seq(-98, -86, 3)
lonlats <- expand.grid(lons, lats)

# Simulate current data (add set.seed for reproducibility)
set.seed(123)
arr.dir <- array(sample(1:360, 1250, replace=T), dim = c(5,5,50))
arr.spd <- array(sample(0:140, 1250, replace=T), dim = c(5,5,50))

Step 3: Plot the Base Map

First, draw your base map and keep the plot active so we can convert geographic coordinates to device coordinates:

plot(lonlats$Var1, lonlats$Var2, xlab="Longitude", ylab="Latitude", asp=1, pch=19, col="blue")
map(database = "world", add=T, fill=T, col="gray"); box()

Step 4: Define a Helper Function to Plot Wind Roses on the Map

Create a reusable function that handles coordinate conversion and wind rose plotting:

plot_wind_rose_on_map <- function(lon, lat, dir, spd, rose_size = 0.15) {
  # Convert geographic coordinates to normalized device coordinates (NDC)
  x_dev <- grconvertX(lon, from = "user", to = "ndc")
  y_dev <- grconvertY(lat, from = "user", to = "ndc")
  
  # Create a grid viewport at the target position
  vp <- viewport(x = x_dev, y = y_dev, width = rose_size, height = rose_size, just = c("center", "center"))
  pushViewport(vp)
  
  # Plot a compact wind rose (adjust parameters as needed)
  windRose(
    mydata = data.frame(ws = spd, wd = dir),
    paddle = FALSE,
    legend = FALSE, # Remove legend to save space
    axes = FALSE, # Hide outer axes
    border = NA, # No border around the rose
    main = NULL, # No title
    col = openair::openColours() # Use openair's default color scheme
  )
  
  popViewport()
}

Step 5: Plot Wind Roses at Target Coordinates

Single Wind Rose (Your Test Point: -86, 30)

# Extract data for the target coordinate
target_lon <- -86
target_lat <- 30
dir_data <- arr.dir[which(lons == target_lon), which(lats == target_lat), ]
spd_data <- arr.spd[which(lons == target_lon), which(lats == target_lat), ]

# Plot the wind rose at this location
plot_wind_rose_on_map(target_lon, target_lat, dir_data, spd_data)

Multiple Wind Roses (e.g., All Grid Points)

Loop through your coordinate list to add roses at every grid point (adjust rose_size to avoid overlap):

for (i in 1:nrow(lonlats)) {
  lon <- lonlats$Var1[i]
  lat <- lonlats$Var2[i]
  
  # Get the corresponding array indices
  lon_idx <- which(lons == lon)
  lat_idx <- which(lats == lat)
  
  # Extract direction and speed data
  dir_data <- arr.dir[lon_idx, lat_idx, ]
  spd_data <- arr.spd[lon_idx, lat_idx, ]
  
  # Plot the wind rose
  plot_wind_rose_on_map(lon, lat, dir_data, spd_data, rose_size = 0.12)
}

Key Customization Tips:

  • Rose Size: Adjust rose_size (0 to 1, where 1 = full plot area) to fit your map density.
  • Speed Binning: Add breaks = seq(0, 140, 20) to windRose to set custom speed intervals.
  • Color Scheme: Replace openColours() with your preferred palette (e.g., viridis::viridis(5)).
  • Add Legends: If you want a shared legend, plot it separately using drawOpenKey() from openair.

This method works by converting geographic coordinates to grid-compatible normalized device coordinates, then drawing each wind rose in its own isolated viewport over the base map.

内容的提问来源于stack exchange,提问作者MilloMarinE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:10:42