Untitled
Anonymous
plain_text
02/21/2026 5:35 AM
3.4 KB
11
Indexable
library(ggplot2)
library(dplyr)
library(tidyr)
library(MBA)
# raw data
df <- data.frame(
y = c(3, 6, 9, 3, 6, 9, 3, 6, 9),
z = c(2, 2, 2, 6.95, 6.95, 6.95, 10.25, 10.25, 10.25),
Vx = c(-0.7, 25, -14, 2, 2, 3, 3, 4, 3),
Vy = c(-0.3, 35, -4, -1, -1, -2, -1, -2, -1.8),
Vz = c(0.1, -6, -3, 0, 0.1, -0.1, 0.5, 0.5, 0.2)
)
# Interpolation Function using MBA
interpolate_velocity_mba <- function(data, variable) {
# formatting data for MBA
xyz <- data.frame(x = data$y, y = data$z, z = data[[variable]])
# we can use MBA to generate a 100x100 smooth surface. 'extend=TRUE' ensures it reaches the edges.
surf <- mba.surf(xyz, no.X = 100, no.Y = 100, extend = TRUE)$xyz.est
# Unpack the surface matrix into a long dataframe for ggplot
df_interp <- expand.grid(y = surf$x, z = surf$y)
df_interp$velocity_val <- as.vector(surf$z)
df_interp$component <- variable
return(df_interp)
}
# processing the data into a format better for plotting
plot_data <- bind_rows(
interpolate_velocity_mba(df, "Vx"),
interpolate_velocity_mba(df, "Vy"),
interpolate_velocity_mba(df, "Vz")
)
# generate the Plot - comment this section out if you dont want the plot that looks like its upside down
# ggplot(plot_data, aes(x = y, y = z, fill = velocity_val)) +
# geom_raster(interpolate = TRUE) +
# scale_fill_distiller(palette = "Spectral",
# name = "cm/s",
# limits = c(-5, 5), # manage outliers - limits on color mapping Keeps smaller variations visible
# oob = scales::squish) +
# facet_wrap(~component, ncol = 3) +
# theme_minimal() +
# theme(panel.background = element_rect(fill = "gray20")) + # Dark background to see edges
# labs(title = "ADV Velocity Fields (MBA Spline Interpolation)",
# x = "Y Position (in)",
# y = "Z Position (in)")
# Generate the Plot with Reversed Z-Axis
ggplot(plot_data, aes(x = y, y = z, fill = velocity_val)) +
geom_raster(interpolate = TRUE) +
scale_fill_distiller(palette = "Spectral",
name = "cm/s",
limits = c(-5, 5),
oob = scales::squish) +
scale_y_reverse() + # THIS IS THE MAGIC LINE
facet_wrap(~component, ncol = 3) +
theme_minimal() +
theme(panel.background = element_rect(fill = "gray20")) +
labs(title = "ADV Velocity Fields (V-Notch Weir Orientation)",
x = "Y Position (in)",
y = "Depth Z (in)") # Updated label to reflect depthEditor is loading...
Leave a Comment