基于ggplot2在R中绘制地下温度时序热图的技术咨询
Hey there! Since you're working with that subsurface temperature dataset (150 depth levels from 0.1m to 15m, 2920 time points in seconds) and already used image.plot, let's refine your heatmap to make it more intuitive and polished. Here are some tailored approaches:
Using
image.plot (from the fields package) First, let's make sure your data is structured correctly and add key improvements like a reversed Y-axis (since deeper levels should sit lower in the plot) and a colorblind-friendly palette.
# Load required packages library(fields) library(viridis) # For accessible color scales # Convert your data frame to a matrix (image.plot works best with matrices) T_mat <- as.matrix(T_mod) # Extract axis values from row/column names depth_vals <- as.numeric(rownames(T_mat)) time_vals <- as.numeric(colnames(T_mat)) # Optional: Convert time from seconds to human-readable dates (assuming start of year) start_date <- as.POSIXct("2023-01-01 00:00:00") time_dates <- start_date + time_vals # Create the heatmap image.plot( x = time_dates, # Use dates instead of raw seconds for clarity y = depth_vals, z = T_mat, xlab = "Date", ylab = "Depth (meters)", main = "Subsurface Temperature Variation Over One Year", col = viridis(100), # Colorblind-friendly palette ylim = rev(range(depth_vals)), # Reverse Y-axis: 0.1m at top, 15m at bottom axes = TRUE ) # Add cleaner X-axis ticks (showing months) axis.POSIXct(1, at = seq(start_date, start_date + 365*24*3600, by = "month"), format = "%b")
Alternative with
ggplot2 If you prefer a more customizable, modern visualization, ggplot2 is a great choice. You'll first reshape your wide-format data frame to long format:
library(tidyverse) # Reshape data from wide to long format T_long <- T_mod %>% rownames_to_column(var = "depth") %>% mutate(depth = as.numeric(depth)) %>% pivot_longer(cols = -depth, names_to = "time_sec", values_to = "temperature") %>% mutate( time_sec = as.numeric(time_sec), time_date = as.POSIXct("2023-01-01 00:00:00") + time_sec # Convert to dates ) # Build the heatmap ggplot(T_long, aes(x = time_date, y = depth, fill = temperature)) + geom_tile() + scale_y_reverse(breaks = seq(0, 15, by = 1)) + # Reverse Y-axis with 1m ticks scale_x_datetime(date_labels = "%b", date_breaks = "1 month") + # X-axis shows months scale_fill_viridis_c(name = "Temperature (°C)") + # Colorblind-friendly color scale labs( x = "Date", y = "Depth (meters)", title = "Subsurface Temperature Over One Year" ) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5), # Center title axis.text.x = element_text(angle = 45, hjust = 1) # Rotate X-axis labels for readability )
Key improvements to note:
- Reversed Y-axis: Aligns with how we intuitively visualize depth (shallower at top, deeper at bottom)
- Viridis color scale: Accessible to colorblind users and avoids common pitfalls of rainbow scales
- Date conversion: Turns raw seconds into readable dates, making the X-axis far easier to interpret
- Cleaner axis ticks: Reduces clutter and highlights meaningful time intervals (months)
内容的提问来源于stack exchange,提问作者Arne Brandschwede
相关产品推荐
相关产品推荐

