You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于ggplot在R中绘制分面多色垂直线展示各类型耗时的技术问询

Solution for ggplot Vertical Line Faceted Plot in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:49:02