Using the function, st_read(), navigate to the shapefile in your file explorer.
## Reading layer `tl_2015_04_cousub' from data source `C:\Users\Josh Carrell\Documents\R-Programming-Portfolio\RPubs\Arizona\tl_2015_04_cousub.shp' using driver `ESRI Shapefile'
## Simple feature collection with 80 features and 18 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -114.8166 ymin: 31.33218 xmax: -109.0452 ymax: 37.00372
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
Using the st_geometry_type() function, you can view the shapefile geometry (Point, Lines, Polygons).
## Geometry set for 80 features
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: -114.8166 ymin: 31.33218 xmax: -109.0452 ymax: 37.00372
## epsg (SRID): 4269
## proj4string: +proj=longlat +datum=NAD83 +no_defs
## First 5 geometries:
## POLYGON ((-113.3542 36.04097, -113.3542 36.0410...
## POLYGON ((-112.1337 35.85596, -112.1337 35.8565...
## POLYGON ((-112.6604 36.53941, -112.6603 36.5396...
## POLYGON ((-111.3669 31.50009, -111.3669 31.5003...
## POLYGON ((-110.9627 31.68695, -110.9625 31.687,...
Using the st_crs(), you can view the coordinate system.
## Coordinate Reference System:
## EPSG: 4269
## proj4string: "+proj=longlat +datum=NAD83 +no_defs"
Using the st_bbox() function, we can view the extent of the bounding box (the 4 coordinates that create a box around the shapefile extent).
## xmin ymin xmax ymax
## -114.81659 31.33218 -109.04517 37.00372
ggplot2 has the functionality to plot shapefiles easily. Using the function geom_sf(data =, size =, color =, fill =) in ggplot() allows you to plot.
Add ggtitle() with "" for a plot title and coord_sf() for x and y coordinates displayed.
ggplot() +
geom_sf(data = AZ, size = .25, color = "Black", fill = "Grey") +
ggtitle("Arizona Counties") +
coord_sf()