ggplot2 geom_path() in R: Connect Points in Data Order
The geom_path() function in ggplot2 connects points with line segments in DATA ROW ORDER (not sorted by x). It is the right tool for trajectory plots, paths, and any sequence where order matters.
ggplot(df, aes(x, y)) + geom_path() ggplot(df, aes(x, y, color = step)) + geom_path() geom_line() # different: sorts by x first geom_path(arrow = arrow()) # add arrow at end
Need explanation? Read on for examples and pitfalls.
What geom_path() does in one sentence
geom_path() connects points with line segments in the ORDER they appear in the data. Unlike geom_line (which sorts by x first), geom_path respects row order.
Syntax
geom_path(mapping = NULL, data = NULL, lineend = "butt", linejoin = "round", linemitre = 10, arrow = NULL, ...).
Five common patterns
1. Random walk
2. Color by step
3. With arrow
4. Multiple paths (groups)
5. Closed loop
geom_path is for SEQUENCED data where the order between points matters. geom_line sorts internally, so it's wrong for trajectories where the path crosses itself.geom_path() vs geom_line() vs geom_step()
| Function | Order | Best for |
|---|---|---|
geom_path() |
Row order | Trajectories, paths |
geom_line() |
Sorted by x | Traditional trends |
geom_step() |
Row order with steps | Piecewise-constant |
geom_polygon() |
Closed loop | Filled shapes |
A practical workflow
Use geom_path for time-series with non-monotonic x.
Plot return vs volatility over time; the PATH shows how each point connects chronologically.
Common pitfalls
Pitfall 1: confusing geom_path with geom_line. geom_path keeps row order; geom_line sorts. For trajectories with non-monotonic x, geom_line is WRONG.
Pitfall 2: forgetting to arrange data. geom_path follows row order. If you want chronological order, arrange by timestamp first.
geom_path() does NOT close the polygon. First and last points are not connected. To close, append the first row to the end before plotting.Try it yourself
Try it: Plot a 2D random walk path with color showing step. Save to ex_plot.
Click to reveal solution
Explanation: geom_path connects points in row (= step) order; color gradient shows direction.
Related ggplot2 functions
After mastering geom_path, look at:
geom_line(): sorted by xgeom_step(): piecewise-constantgeom_polygon(): closed shapegeom_segment(): explicit segmentsarrow(): add direction markers
FAQ
What does geom_path do in ggplot2?
geom_path() connects points with line segments in DATA ROW ORDER. Unlike geom_line, it does not sort by x.
What is the difference between geom_path and geom_line?
geom_path keeps row order. geom_line sorts by x. For trajectories or non-monotonic x, geom_path is correct.
How do I add an arrow to geom_path?
Pass arrow = arrow(length = unit(0.3, "cm")): geom_path(arrow = arrow()). An arrowhead appears at the end.
Does geom_path close polygons?
No. First and last points are NOT connected. For closed loops, append the first row to the end of the data before plotting (or use geom_polygon).
Can I use geom_path for time-series?
Yes, especially when (x, y) are both varying and the chronological order matters. For x = time, y = value with monotonic time, geom_line is fine.