# Function to generate vectors
generate_vectors <- function(v) {
x <- v[1]
y <- v[2]
c(
sin(x) + sin(y) + rnorm(1, 5, 1),
sin(x) - sin(y) - rnorm(1, 5, 1)
)
}
# Set seed for reproducibility
set.seed(123)
# Create sample points and compute vectors
sample_points <- data.frame(
x = runif(30, 0, 10),
y = runif(30, 0, 10)
)
result <- t(apply(sample_points, 1, generate_vectors))
sample_points$xend <- result[, 1]
sample_points$yend <- result[, 2]
sample_points$fx <- sample_points$xend - sample_points$x
sample_points$fy <- sample_points$yend - sample_points$y
sample_points$distance <- sqrt(sample_points$fx^2 + sample_points$fy^2)
sample_points$angle <- atan2(sample_points$fy, sample_points$fx)
# Define evaluation points
eval_points <- data.frame(
x = c(0, 7.5),
y = c(10, 5)
)
# Example 1:
ggplot(sample_points, aes(x = x, y = y)) +
geom_vector(aes(fx = fx, fy = fy, color = NULL), center = FALSE, alpha = 0.2) +
geom_vector_smooth(aes(fx = fx, fy = fy), n = 5) +
ggtitle("Smoothed Vector Field")
# Example 2: Ellipse with eval_points
ggplot(sample_points, aes(x = x, y = y)) +
geom_vector(aes(fx = fx, fy = fy, color = NULL), center = FALSE, alpha = 0.2) +
geom_vector_smooth(aes(fx = fx, fy = fy), eval_points = eval_points, conf_level = c(0.9)) +
ggtitle("Smoothed Vector Field with Ellipse Intervals")
# Example 3: Wedge with eval_points
ggplot(sample_points, aes(x = x, y = y)) +
geom_vector(aes(fx = fx, fy = fy, color = NULL), center = FALSE, alpha = 0.2) +
geom_vector_smooth(aes(fx = fx, fy = fy), eval_points = eval_points, pi_type = "ellipse") +
ggtitle("Smoothed Vector Field with Wedge Intervals")
Run the code above in your browser using DataLab