contour: Contour functions for tidy and geospatial kernel estimates

contourR Documentation

Contour functions for tidy and geospatial kernel estimates

Description

Contour functions for tidy and geospatial kernel estimates.

Usage

## S3 method for class 'tidy_ks'
contourLevels(x, cont=c(25,50,75), group=FALSE, ...)
## S3 method for class 'sf_ks'
contourLevels(x, cont=c(25,50,75), group=FALSE, ...)
contour_breaks(data, cont=c(25,50,75), group=FALSE)
label_percent(y)

st_get_contour(x, cont=c(25,50,75), breaks, which_deriv_ind, disjoint=TRUE,
    as_point=FALSE)

Arguments

x, data

tidy kernel estimate (output from tidy_k*) or geospatial kernel estimate (output from st_k*)

cont

vector of contour levels. Default is c(25,50,75).

group

flag to compute contour levels per group. Default is FALSE.

breaks

tibble or vector of contour levels (e.g. output from contour_breaks)

which_deriv_ind

derivative index (only required for st_kdde objects)

disjoint

flag to compute disjoint contours. Default is TRUE.

as_point

flag to return polygons as point coordinates in tidy format. Default is TRUE.

y

factor variable

...

other parameters (not implemented)

Details

By default, the 1% to 99% contours are computed for an st_k* output, though a plot of all 99 of them would be too crowded. st_get_contour selects a subset of these, as specified by cont. If a contour level in cont does not already exist or if absolute contour levels are specified in breaks, then the corresponding contours are computed. If disjoint=TRUE (default) then the contours are computed as a set of disjoint multipolygons: this allows for plotting without overlapping transparent colours. If disjoint=FALSE then the contours are overlapping and so their colours alpha-mixed, but they strictly satisfy the probabilistic definition, e.g. a 25% contour region is the smallest region that contains 25% of the probability mass defined by the kernel estimate, see geom_contour_ks.

Since these default probability contours are relative contour levels, they aren't suitable for producing a contour plot with fixed contour levels across all groups. It may require trial and error to obtain a single set of contour levels which is appropriate for all groups: one possible choice is provided by contour_breaks.

Value

The output from contour_breaks is a tibble of the values of the contour breaks. The output from st_get_contour is an sf object of the contours as multipolygons.

See Also

geom_contour_ks

Examples

library(ggplot2)
data(crabs, package="MASS")
crabs2 <- dplyr::select(crabs, FL, CW, sex)
crabs2 <- dplyr::group_by(crabs2, sex)
t1 <- tidy_kde(crabs2)
b <- contour_breaks(t1)
ggplot(t1, aes(x=FL, y=CW)) + 
    geom_contour_filled_ks(colour=1, breaks=b) + facet_wrap(~sex)

crabs3 <- dplyr::select(crabs, FL, CW)
t2 <- tidy_kde(crabs3)
ggplot(t2, aes(x=FL, y=CW)) + 
    geom_contour_filled_ks(colour=1, cont=c(50,75,97.5))

## extract contour polygons
crabs2s <- sf::st_as_sf(crabs2, coords=c("FL","CW"))
t2 <- st_kde(crabs2s)
t2 <- st_get_contour(t2, breaks=b, as_point=TRUE)
t2 <- dplyr::rename(t2, FL=X, CW=Y)
ggplot(t2, aes(x=FL, y=CW)) + 
    geom_polygon(aes(fill=contlabel, subgroup=contlabel_group), col=1) + 
    scale_fill_viridis_d() + guides(fill=guide_legend(reverse=TRUE)) + 
    facet_wrap(~sex) 

eks documentation built on May 29, 2024, 1:17 a.m.