Select / subset spatial data in R

1.5k views Asked by At

I am working on a large data set with spatial data (lat/long). My data set contains some positions that I don´t want in my analysis (it makes the files to heavy to process in ArcMap- many Go of data).

This is why I want to subset the relevant data for my work. Here is an example of my data:

lat        long
59.979687  29.706236
60.136177  28.148186
59.331383  22.376234
57.699154  11.667305
54.90566   17.768538
57.122417  8.59762
55.8108    10.820534
57.86072   22.616697
59.912144  30.24504

The data I have are from all of the area covered in the following map. I would like to work only on the Baltic Sea, so (almost) only with the points on the left of this dark line. map

Does someone heard about a package or has some ideas about how to deal with this?

An other solution might be to drawn a polygon around the Baltic Sea and only to select the points within this polygon.

1

There are 1 answers

1
hrbrmstr On BEST ANSWER

I'm going with the assumption you meant "to the right" since you said "Another solution might be to drawn a polygon around the Baltic Sea and only to select the points within this polygon"

# your sample data

pts <- read.table(text="lat        long
59.979687  29.706236
60.136177  28.148186
59.331383  22.376234
57.699154  11.667305
54.90566   17.768538
57.122417  8.59762
55.8108    10.820534
57.86072   22.616697
59.912144  30.24504", header=TRUE)

library(ggplot2)
library(sp)
library(rgdal)

# a rough baltic polygon that I deliberately made to exclude one point

baltic <- data.frame(lat=c(69.91, 57.98, 51.0, 60, 69.91),
                     long=c(23.07, 7.05, 12.0, 30, 23.07))

# only doing this to show the points, you can filter out in similar fashion

pts$is_in <- factor(point.in.polygon(pts$long, pts$lat, baltic$long, baltic$lat))

# I made an _extremely_ oversimplified map of europe for speedy plotting
europe <- readOGR("http://rud.is/dl/simple_europe.geojson", "OGRGeoJSON")
europe_map <- fortify(europe)


# plot to show it works
gg <- ggplot()
gg <- gg + geom_map(map=europe_map, data=europe_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="white", color="black", size=0.25)
gg <- gg + geom_point(data=pts, aes(x=long, y=lat, color=is_in))
gg <- gg + geom_path(data=baltic, (aes(x=long, y=lat)), linetype=2)

# lambert isn't a bad projection for the region
gg <- gg + coord_map("lambert", lat0=50, lat1=70, xlim=c(5,31), ylim=c(50, 70))
gg

enter image description here

You can make the polygon as big as you need to get the points you want to include. Remember, I made it just small enough to exclude that one "red" point on purpose.