Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong vertex order in geom_line plot

Tags:

r

ggplot2

Getting a strange ordering of vertices in a geom_line 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 lines() confirms.

like image 942
geotheory Avatar asked Oct 01 '16 21:10

geotheory


1 Answers

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

like image 192
eipi10 Avatar answered Sep 22 '22 23:09

eipi10