Get contour lines, using a wrapper for contourLines

get_contourlines(df = NULL, nlevels = 10, drop_outer = TRUE,
  levels = NULL, ..., type = "data.frame", crs = 4326)

Arguments

df

A data.frame containing x, y, and z columns. x and y must form a raster, meaning every x must be represented for each y, and vice versa.

nlevels

An integer containing number of levels to return. Used IFF levels not supplied.

drop_outer

If TRUE, nlevels+2 will be calculated, dropping the outer 2 values. Used IFF levels not supplied.

levels

A vector of values at which contours should be plotted. If used, nlevels is ignored.

...

Should contain x, y, and z, and then df will be ignored.

type

Should be either "data.frame" or "sf"

crs

The target crs for "sf" output. Used as input in sf::st_as_sf

Examples

# Example 1 df <- expand.grid(x=-10:10,y=-10:10) df$z <- df$x^2 cl <- get_contourlines(df,nlevels=5) unique(cl$level)
#> [1] 16.66667 33.33333 50.00000 66.66667 83.33333
library(ggplot2) ggplot() + geom_raster(data=df,aes(x,y,fill=z)) + geom_path(data=cl,aes(x,y,group=line))
cl <- get_contourlines(df,levels=c(15,20,60)) unique(cl$level)
#> [1] 15 20 60
# Example 2, with "sf" object library(sf)
#> Warning: package ‘sf’ was built under R version 3.6.2
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
df <- expand.grid(x=seq(-5,5,length.out=20),y=seq(-5,5,length.out=20)) df$z <- sqrt(df$x^2+df$y^2) cl <- get_contourlines(df,levels=seq(1,10,by=1.5), type="sf") ggplot() + geom_raster(data=df,aes(x,y,fill=z)) + geom_sf(data=cl,aes())