geotheory geotheory - 6 months ago 45
R Question

Wrong vertex order in geom_line plot

Getting a strange ordering of vertices in a

plot. Left hand plot is base R; right is ggplot.

enter image description here

Here's the shapefile I'm working with. This will reproduce the plot:

require(ggplot2); require(maptools)
rail = readShapeLines('railnetworkLine.shp')
rail_dat = fortify(rail[1,])
ggplot(rail_dat) + geom_line(aes(long, lat, group=group)) + coord_equal()

Any idea what is causing this? The data order of fortify seems correct, as plotting separately


Use geom_path instead of geom_line. geom_line orders the data from lowest to highest x-value (long in this case) before plotting, but geom_path plots the data in the current order of the data frame rows.

ggplot(rail_dat) + 
  geom_path(aes(long, lat)) + coord_equal()

enter image description here