| geom_contour_ks {eks} | R Documentation |
Contour and filled contour plots for tidy kernel estimates
Description
Contour and filled contour plots for tidy kernel estimates for 2-dimensional data.
Usage
geom_contour_ks(mapping=NULL, data=NULL, stat="contour_ks",
position="identity", ..., cont=c(25,50,75), label_percent=NULL,
breaks=NULL, show.legend=NA, inherit.aes=TRUE)
stat_contour_ks(mapping=NULL, data=NULL, geom="contour_ks",
position="identity", ..., cont=c(25,50,75), label_percent=NULL,
breaks=NULL, show.legend=NA, inherit.aes=TRUE)
geom_contour_filled_ks(mapping=NULL, data=NULL, stat="contour_filled_ks",
position="identity", ..., cont=c(25,50,75), label_percent=NULL,
breaks=NULL, show.legend=NA, inherit.aes=TRUE)
stat_contour_filled_ks(mapping=NULL, data=NULL, geom="contour_filled_ks",
position="identity", ..., cont=c(25,50,75), label_percent=NULL,
breaks=NULL, show.legend=NA, inherit.aes=TRUE)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
cont |
Vector of contour probabilities. Default value is |
label_percent |
Flag for legend label as percentage. Default is TRUE. |
breaks |
Numeric vector to set the contour breaks e.g. output from |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom |
The geometric object to use display the data. |
Details
These layer functions are modifications of the standard layer functions ggplot2::geom_contour, geom_contour_filled and ggplot2::stat_contour, stat_contour_filled. Their usage and output are similar, except that they require a tidy kernel estimate as the input, rather than the observations themselves, and that the underlying choice of the contour levels is different. For most cases, geom_contour_ks is equivalent to geom_contour(stat="contour_ks"), and likewise for geom_contour_filled_ks.
The choice of the contour levels are based on probability contours. A 25% contour region is the smallest region that contains 25% of the probability mass defined by the kernel estimate. Probability contours offer a more intuitive approach to selecting the contour levels that reveal the pertinent characteristics of the kernel estimates. See Chacon & Duong (2018, Chapter 2.2). They are specified by the cont parameter: the default value is cont=c(25,50,75), which computes the upper quartile, median and lower quartile probability contours. If percent_label=TRUE, then the legend labels are given as these percentage in cont. Otherwise, the labels are the contour levels themselves.
Since these probability contours are computed for each group of the grouping variable in data, then these relative contour levels are different for each group. To produce a contour plot with fixed contour levels across all groups, then these can be supplied in breaks: a possible choice is provided by contour_breaks.
Value
Similar output as the standard layer functions ggplot2::geom_contour, geom_contour_filled and ggplot2::stat_contour, stat_contour_filled.
References
Chacon, J.E. & Duong, T. (2018) Multivariate Kernel Smoothing and Its Applications. Chapman & Hall/CRC, Boca Raton.
See Also
Examples
library(ggplot2)
data(crabs, package="MASS")
crabs2 <- dplyr::select(crabs, FL, CW, sp)
crabs2 <- dplyr::group_by(crabs2, sp)
tt <- tidy_kde(crabs2)
gt <- ggplot(tt, aes(x=FL, y=CW))
gt + geom_contour_ks() + facet_wrap(~sp)
gt + geom_contour(stat="contour_ks") + facet_wrap(~sp) ## same output
gt + geom_contour_filled_ks(colour=1) + facet_wrap(~sp)
gt + geom_contour_filled(stat="contour_filled_ks", colour=1) +
facet_wrap(~sp) ## same output