R编程技术问询:生成随机米值关联DFT并实现3D管道可视化
Hey there! Let's work through your two R programming tasks step by step—super cool that you're diving into your first project with real-world data!
Task 1: Generate Distance Vector, Link to DFT Data, and Create Grouped Line Plot
First, we'll use the tidyverse package (it combines data manipulation and plotting tools, perfect for this job). If you don't have it installed yet, run install.packages("tidyverse") first.
Step 1: Set Up Your Data
First, let's recreate your sample data frame (you can replace this with your full 2000-row dataset):
library(tidyverse) # Sample DFT data dft_data <- tibble( Quarter = c(rep("1st", 3), rep("2nd", 4), rep("3rd", 5)), DFT = c(1.61, 2.35, 1.74, 2.56, 1.79, 1.84, 1.69, 1.85, 1.73, 2.62, 2.43, 1.85) )
Step 2: Generate & Link the Distance Vector
We need a random vector between 0-3 meters, with the rule: closer to 0 = lower DFT. To enforce this, we'll sort both the distances and DFT values, then pair them up:
# Generate 0-3 random distances (one per row) set.seed(123) # Optional: makes random values reproducible distances <- runif(nrow(dft_data), min = 0, max = 3) # Sort distances and DFT to enforce inverse relationship, then add to data frame dft_data <- dft_data %>% arrange(DFT) %>% # Sort DFT from lowest to highest mutate(Distance_m = sort(distances, decreasing = FALSE)) %>% # Match with smallest distances first arrange(Quarter, Distance_m) # Re-sort for plotting
The set.seed() line ensures you get the same random values every time you run the code—great for testing!
Step 3: Plot the Grouped Line Chart
Now we'll make a line plot grouped by Quarter, with Distance on the x-axis and DFT on the y-axis:
dft_plot <- ggplot(dft_data, aes(x = Distance_m, y = DFT, color = Quarter)) + geom_line(linewidth = 1.2) + geom_point(size = 2) + labs( title = "DFT vs. Distance by Quarter", x = "Distance (meters)", y = "Dry Film Thickness", color = "Quarter" ) + theme_minimal() + ylim(0, max(dft_data$DFT) + 0.2) # Adjust y-axis to fit all values print(dft_plot)
This plot will show clear trends: for each quarter, as distance increases, DFT increases (following your inverse rule for the pairing).
Task 2: Convert to 3D Cylinder Visualization (Replace Quarter with Perimeter)
For 3D pipe/cylinder visualization, we'll use the rgl package—it's designed for interactive 3D graphics in R. Install it first with install.packages("rgl").
Step 1: Map Quarter to Perimeter Angles
We'll treat the pipe as a cylinder, where each quarter corresponds to a position around the pipe's perimeter. Let's assign angles (in radians) to each quarter:
library(rgl) # Map Quarter to perimeter angle (0 = start of pipe, 2π = full circle) dft_data <- dft_data %>% mutate( Angle_rad = case_when( Quarter == "1st" ~ 0, Quarter == "2nd" ~ pi/2, # 90 degrees Quarter == "3rd" ~ pi, # 180 degrees # Add 4th quarter if needed: Quarter == "4th" ~ 3*pi/2 ) )
Step 2: Calculate 3D Cylinder Coordinates
For a cylinder, we can convert our distance (axial position, Z-axis) and perimeter angle (X/Y axes) to 3D coordinates. We'll also use DFT to "raise" points off the cylinder surface to show thickness:
# Pipe radius (adjust this to make the cylinder wider/narrower) pipe_radius <- 1 # Calculate X/Y/Z coordinates dft_data <- dft_data %>% mutate( # Base cylinder coordinates X_base = pipe_radius * cos(Angle_rad), Y_base = pipe_radius * sin(Angle_rad), Z_base = Distance_m, # Adjust coordinates by DFT to show thickness X = X_base + (DFT/10) * cos(Angle_rad), # Scale DFT so it's visible Y = Y_base + (DFT/10) * sin(Angle_rad), Z = Z_base )
The DFT/10 scaling ensures the thickness doesn't make the plot look distorted—tweak this number if needed!
Step 3: Create the 3D Visualization
Now we'll draw the pipe cylinder and overlay the DFT points:
# Open 3D plotting window open3d() # Draw the base pipe cylinder (transparent so we can see the DFT points) cylinder3d( center = c(0, 0, 0), radius = pipe_radius, length = max(dft_data$Distance_m), sides = 100, # Smoothness of the cylinder color = "lightgray", alpha = 0.3 ) # Add DFT points (colored by quarter) points3d( x = dft_data$X, y = dft_data$Y, z = dft_data$Z, col = dft_data$Quarter, size = 5 ) # Add axis labels and title axes3d() title3d(main = "3D Pipe DFT Visualization", xlab = "X (Perimeter)", ylab = "Y (Perimeter)", zlab = "Distance (m)") legend3d("topright", legend = unique(dft_data$Quarter), col = unique(dft_data$Quarter), pch = 16)
You can rotate, zoom, and pan the 3D plot with your mouse to explore the data from all angles!
内容的提问来源于stack exchange,提问作者user3407156

