基于ggplot在R中绘制分面多色垂直线展示各类型耗时的技术问询
Hey there! Let's fix this up so you get that Excel-style vertical line plot you're aiming for. The main hurdles right now are your wide-format data (ggplot2 works best with long/tidy data) and using the right geom to create those clean vertical lines instead of full bars.
Step 1: Load Required Packages
First, make sure you have the tidyverse package installed (it includes ggplot2 for plotting and tidyr for reshaping data):
library(tidyverse)
Step 2: Define Your Dataset
Here's your dataset as provided (formatted for readability):
df <- data.frame( type = c("X", "Y", "Z"), A_to_B = c(20, 56, 57), B_to_C = c(10, 35, 50), C_to_D = c(53, 20, 58) )
Step 3: Reshape Data to Long Format
We need to convert your wide data into long format so ggplot can easily map segments to positions. We'll also add an x column to assign numerical positions to each road segment (this helps place our vertical lines correctly):
df_long <- df %>% pivot_longer( cols = -type, names_to = "segment", values_to = "time" ) %>% mutate( x = case_when( segment == "A_to_B" ~ 1, segment == "B_to_C" ~ 2, segment == "C_to_D" ~ 3 ) )
Step 4: Create the Vertical Line Plot
We have two options depending on exactly what you mean by "vertical lines at segment connections":
Option 1: Vertical Lines from X-Axis to Segment Time (Excel-Style)
This creates vertical lines starting at the x-axis (y=0) and extending up to each segment's time (like a bar chart but just the line, no fill):
ggplot(df_long, aes(x = x, y = time)) + # Draw vertical lines from y=0 to the segment time geom_segment(aes(xend = x, yend = 0), color = "steelblue", size = 1.2) + # Optional: Add points at the top of each line for clarity geom_point(color = "darkblue", size = 3) + # Split into facets by type facet_wrap(~type) + # Label x-axis with segment names instead of numbers scale_x_continuous(breaks = c(1, 2, 3), labels = c("A→B", "B→C", "C→D")) + # Add labels and title labs( x = "Road Segment", y = "Time (Units)", title = "Road Segment Time by Type" ) + # Clean up the theme theme_minimal()
Option 2: Only Vertical Connecting Lines Between Segments
If you want only the vertical lines connecting consecutive segments (no lines from the x-axis, just the links between segment times), we first calculate the previous segment's time and the midpoint between segments, then draw those vertical connections:
# Prepare data to get previous segment times and midpoints df_connected <- df_long %>% group_by(type) %>% mutate( prev_time = lag(time), # Get time of the previous segment mid_x = x - 0.5 # Calculate midpoint between current and previous segment ) %>% drop_na() # Remove the first segment (no previous segment to connect to) # Plot with only connecting vertical lines ggplot(df_long, aes(x = x, y = time)) + # Show the segment time points geom_point(color = "darkblue", size = 3) + # Draw vertical connecting lines between segments geom_segment( data = df_connected, aes(x = mid_x, xend = mid_x, y = prev_time, yend = time), color = "steelblue", size = 1.2 ) + facet_wrap(~type) + scale_x_continuous(breaks = c(1, 2, 3), labels = c("A→B", "B→C", "C→D")) + labs( x = "Road Segment", y = "Time (Units)", title = "Road Segment Time Connections by Type" ) + theme_minimal()
Explanation
- Option 1 mimics Excel's standard vertical line chart, where each segment's time is represented by a vertical line from the axis.
- Option 2 focuses only on the connections between segments, which matches your description of "retaining only the vertical lines at the connections of each segment's time, not full bars".
内容的提问来源于stack exchange,提问作者Harshal Gajare

