inst/doc/sf5.R

## ----echo=FALSE, include=FALSE-------------------------------------------
knitr::opts_chunk$set(collapse = TRUE)
mostly_off = Sys.getenv("USER") == "edzer"

## ------------------------------------------------------------------------
library(sf)
demo(nc, ask = FALSE, echo = FALSE)
plot(st_geometry(nc))

## ------------------------------------------------------------------------
plot(st_geometry(nc), col = sf.colors(12, categorical = TRUE), border = 'grey', 
	 axes = TRUE)
plot(st_geometry(st_centroid(nc)), pch = 3, col = 'red', add = TRUE)

## ------------------------------------------------------------------------
plot(nc)

## ------------------------------------------------------------------------
plot(nc, max.plot = 14)

## ------------------------------------------------------------------------
options(sf_max.plot=1)
plot(nc)

## ------------------------------------------------------------------------
plot(nc["AREA"])

## ------------------------------------------------------------------------
plot(nc["AREA"], key.pos = 4)

## ------------------------------------------------------------------------
plot(nc["AREA"], key.pos = 1, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)

## ------------------------------------------------------------------------
nc$f = cut(nc$AREA, 10)
plot(nc["f"], axes = TRUE, key.pos = 4, pal = sf.colors(10), key.width = lcm(4.5))

## ------------------------------------------------------------------------
plot(nc["AREA"], breaks = c(0,.05,.1,.15,.2,.25))

## ------------------------------------------------------------------------
plot(nc["AREA"], breaks = "jenks")

## ------------------------------------------------------------------------
plot(st_geometry(nc), axes = TRUE)

## ------------------------------------------------------------------------
lat_ts = mean(st_bbox(nc)[c(2,4)]) # latitude of true scale
eqc = st_transform(nc, paste0("+proj=eqc +lat_ts=", lat_ts))
plot(st_geometry(eqc), axes = TRUE)

## ------------------------------------------------------------------------
library(maps)
usa = st_as_sf(map('usa', plot = FALSE, fill = TRUE))
laea = st_crs("+proj=laea +lat_0=30 +lon_0=-95") # Lambert equal area
usa <- st_transform(usa, laea)
g = st_graticule(usa)
plot(st_geometry(g), axes = TRUE)

## ------------------------------------------------------------------------
plot(usa, graticule = TRUE, key.pos = NULL, axes = TRUE)

## ------------------------------------------------------------------------
g = st_graticule(usa, lon = seq(-130,-65,5))
plot(usa, graticule = g, key.pos = NULL, axes = TRUE,
	 xlim = st_bbox(usa)[c(1,3)], ylim = st_bbox(usa)[c(2,4)],
	 xaxs = "i", yaxs = "i")

## ------------------------------------------------------------------------
methods(st_as_grob)

## ------------------------------------------------------------------------
library(ggplot2)
ggplot() + geom_sf(data = usa)

## ------------------------------------------------------------------------
ggplot() + 
  geom_sf(data = nc, aes(fill = BIR74)) + 
  scale_y_continuous(breaks = 34:36)

## ------------------------------------------------------------------------
library(dplyr)
library(tidyr)
nc2 <- nc %>% select(SID74, SID79, geom) %>% gather(VAR, SID, -geom)
ggplot() + 
  geom_sf(data = nc2, aes(fill = SID)) + 
  facet_wrap(~VAR, ncol = 1) +
  scale_y_continuous(breaks = 34:36)

## ----eval=mostly_off-----------------------------------------------------
library(mapview)
mapview(nc["BIR74"], col.regions = sf.colors(10))

## ------------------------------------------------------------------------
library(tmap)
qtm(nc)

## ----eval=mostly_off-----------------------------------------------------
tmap_mode("view")
tm_shape(nc) + tm_fill("BIR74", palette = sf.colors(5))

## ----eval=mostly_off-----------------------------------------------------
ttm()
last_map()

Try the sf package in your browser

Any scripts or data that you put into this service are public.

sf documentation built on July 24, 2019, 5:05 p.m.