library(sp) library(rgeos) library(ggplot2) # Create a line and a polygon line <- Lines(list(Line(cbind(c(1, 4), c(3, 7)))),"line") poly <- Polygons(list(Polygon(cbind(c(2, 4, 5, 3), c(4, 7, 6, 5)))),"poly") sl <- SpatialLines(list(line)) spol <- SpatialPolygons(list(poly)) # Find the intersection between the line and the polygon intersection <- gIntersection(sl, spol) # Convert the intersection to a data frame df <- data.frame(intersection@coords) # Create a ggplot of the intersection ggplot(df, aes(x = X1, y = X2)) + geom_path() + ggtitle("Intersection of Line and Polygon") # Save the plot as a PDF file ggsave("intersection_plot.pdf")