filled.contour未完整绘制矩阵(Y轴被截断)求助
filled.contour() Truncating Your Dataset at a Specific Y-Value It sounds frustrating when your interpolation checks out but the plot cuts off unexpectedly—let’s walk through some practical fixes for this issue:
1. Force the Plot to Use Your Full Interpolated Range
filled.contour() often auto-adjusts axis ranges based on non-NA data, which can lead to truncation if there’s a stretch of NA values along a Y-axis slice. Explicitly define the x and y limits using the full range of your interpolated grid to override this behavior:
# Extract the full range from your interp output x_full_range <- range(Sr.All_ln$x) y_full_range <- range(Sr.All_ln$y) # Redraw the plot with explicit limits filled.contour(Sr.All_ln, xlim = x_full_range, ylim = y_full_range, plot.axes = { axis(1, at = pretty(x_full_range)) axis(2, at = pretty(y_full_range)) points(Sr.All$Lon, Sr.All$Lat, pch = 16) })
2. Work Around filled.contour()'s Layout Quirks
Under the hood, filled.contour() uses layout() to split the plot window into the main contour area and color bar. This can sometimes mess with axis rendering when adding custom plot.axes elements. A more reliable alternative is to use ggplot2, which handles layouts more intuitively:
library(ggplot2) # Convert your interp output to a tidy data frame interp_df <- expand.grid(Lon = Sr.All_ln$x, Lat = Sr.All_ln$y) interp_df$Sr_isot <- as.vector(Sr.All_ln$z) # Build the plot ggplot(interp_df, aes(x = Lon, y = Lat, fill = Sr_isot)) + geom_raster() + # Renders the interpolated grid geom_point(data = Sr.All, aes(x = Lon, y = Lat), pch = 16, inherit.aes = FALSE) + # Adds original points scale_fill_viridis_c() + # Customizable color scale coord_equal() # Preserves spatial aspect ratio
3. Double-Check for Hidden NA Patterns
Even if your matrix looks "normal," a row of consecutive NA values at the problematic Y-slice could cause filled.contour() to truncate. Verify this with a quick check:
# Count NA values in each row of your interpolated z matrix row_na_counts <- rowSums(is.na(Sr.All_ln$z)) print(row_na_counts) # If you spot full-NA rows, enable extrapolation in interp() to fill edge values Sr.All_ln <- interp(Sr.All$Lon, y = Sr.All$Lat, z = Sr.All$`Sr isot`, duplicate = "mean", extrap = TRUE)
The extrap=TRUE parameter fills in values beyond your original data range, eliminating the full-NA rows that trigger truncation.
内容的提问来源于stack exchange,提问作者CecileB

