# This example uses the "sf" package to read in an external ESRI shapefile for Maine
if (require(sf)) {
# download example shapefile zip from github, and unzip
zippath <- paste(tempdir(),"Income_schooling.zip",sep="/")
download.file("https://github.com/mgimond/Spatial/raw/main/Data/Income_schooling.zip",
destfile = zippath, mode='wb')
unzip(zippath, exdir = tempdir())
# read shapefile into sf format
shppath <- paste(tempdir(),"Income_schooling.shp",sep="/")
basemap0 <- st_read(shppath)
# Create example data by randomly sampling within bounding box
rs <- st_bbox(basemap0) # get ranges of X and Y
MEdata <- data.frame(X=runif(300,rs[1],rs[3]), Y=runif(300,rs[2],rs[4]))
plot(basemap0["NAME"], reset=FALSE)
plot(st_as_sf(MEdata,coords=1:2), add=TRUE) # plot data in black
# trim data to basemap, and plot trimmed data with red X's
dME <- trimdata(MEdata, basemap0)
plot(st_as_sf(dME,coords=1:2), col="red", pch="X", add=TRUE)
dev.off() # clear map settings
}
# The following examples use the "maps" package, which includes its own maps
if (require(maps)) {
data(beertweets)
### Trim beer tweet data to US base map, and plot them
basemap1 <- map("usa", fill=TRUE, col="transparent")
dUS <- trimdata(beertweets, basemap1)
# Plot tweet locations (beer tweets in red)
points(dUS$longitude, dUS$latitude, col=dUS$beer+1, cex=0.5)
dev.off() # clear map settings
### Trim beer tweet data to Texas base map, and plot them
basemap2 <- map("state", regions="texas", fill=TRUE, col="transparent")
dTX <- trimdata(beertweets, basemap2)
# Plot tweet locations (beer tweets in red)
points(dTX$longitude, dTX$latitude, col=dTX$beer+1, cex=0.5)
}
Run the code above in your browser using DataLab