0

I have been trying to load and plot a few shapefile in R using ggplot() and am facing an issue with a line vector shapefile. It seems that the readOGR() command is importing the line vectors ordering the points of the lines in the x-axis order so that the plot does not correspond to its correct shape. The "curvy" part of the lines are plotted wrongly, especially those in the right-most part of the plot.

The first figure attached illustrates how that shapefile is, plotted in QGIS. The second the output by loading the .shp with readOGR() and plotting it with ggplot(). I do so by running

library(rgdal)
library(ggplot2)
areas <- readOGR('.', 'areas')
ggplot(fortify(areas)) + geom_line(aes(long, lat, group = group))

Any thoughts on how to fix it? Here the source shapefile.

plot in QGIS enter image description here

1 Answer 1

4

The issue is how you plot the lines. From the geom_path/geom_line help page:

geom_path() connects the observations in the order in which they appear in the data. geom_line() connects them in order of the variable on the x axis.

Therefore, you should use geom_path to plot the shapefile as it is:

library(rgdal)
library(ggplot2)
areas <- readOGR('.', 'areas')
ggplot(fortify(areas)) + geom_path(aes(long, lat, group = group))

giving: enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.