## tidy density estimates
library(ggplot2)
data(crabs, package="MASS")
## tidy 1-d density estimate per species
crabs1 <- dplyr::select(crabs, FL, sp)
crabs1 <- dplyr::group_by(crabs1, sp)
t1 <- tidy_kde(crabs1)
gt1 <- ggplot(t1, aes(x=FL))
gt1 + geom_line(colour=1) + geom_rug_ks(colour=4) + facet_wrap(~sp)
## tidy 2-d density estimate
## suitable smoothing matrix gives bimodal estimate
crabs2 <- dplyr::select(crabs, FL, CW)
t2 <- tidy_kde(crabs2)
gt2 <- ggplot(t2, aes(x=FL, y=CW))
gt2 + geom_contour_filled_ks(colour=1) +
colorspace::scale_fill_discrete_sequential()
## default smoothing matrix in geom_density_2d_filled() gives unimodal estimate
gt3 <- ggplot(crabs2, aes(x=FL, y=CW))
gt3 + geom_density_2d_filled(bins=4, colour=1) +
colorspace::scale_fill_discrete_sequential() +
guides(fill=guide_legend(title="Density", reverse=TRUE))
## facet wrapped geom_sf plot with fixed contour levels for all facets
crabs3 <- dplyr::select(crabs, FL, CW, sex)
t3 <- tidy_kde(dplyr::group_by(crabs3, sex))
b <- contour_breaks(t3)
gt3 <- ggplot(t3, aes(x=FL, y=CW))
gt3 + geom_contour_filled_ks(colour=1, breaks=b) +
colorspace::scale_fill_discrete_sequential() + facet_wrap(~sex)
## geospatial density estimate
data(wa)
data(grevilleasf)
hakeoides <- dplyr::filter(grevilleasf, species=="hakeoides")
hakeoides_coord <- data.frame(sf::st_coordinates(hakeoides))
s1 <- st_kde(hakeoides)
## base R plot
xlim <- c(1.2e5, 1.1e6); ylim <- c(6.1e6, 7.2e6)
plot(wa, xlim=xlim, ylim=ylim)
plot(s1, add=TRUE)
## geom_sf plot
## suitable smoothing matrix gives optimally smoothed contours
gs1 <- ggplot(s1) + geom_sf(data=wa, fill=NA) + ggthemes::theme_map() +
colorspace::scale_fill_discrete_sequential(palette="Heat2")
gs1 + geom_sf(data=st_get_contour(s1), aes(fill=label_percent(contlabel))) +
coord_sf(xlim=xlim, ylim=ylim)
## default smoothing matrix in geom_density_2d_filled() is oversmoothed
gs2 <- ggplot(hakeoides_coord) + geom_sf(data=wa, fill=NA) +
ggthemes::theme_map()
gs2 + geom_density_2d_filled(aes(x=X, y=Y), bins=4, colour=1) +
colorspace::scale_fill_discrete_sequential(palette="Heat2") +
guides(fill=guide_legend(title="Density", reverse=TRUE)) +
coord_sf(xlim=xlim, ylim=ylim)
if (FALSE) ## export as geopackage for external GIS software
sf::write_sf(wa, dsn="grevillea.gpkg", layer="wa")
sf::write_sf(hakeoides, dsn="grevillea.gpkg", layer="hakeoides")
sf::write_sf(gs1_cont, dsn="grevillea.gpkg", layer="hakeoides_cont")
sf::write_sf(s1$grid, dsn="grevillea.gpkg", layer="hakeoides_grid")
Run the code above in your browser using DataLab