Skip to content

Scaled densities/counts in 2d density/bins plots (#2679) #2680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ggplot2 3.0.0.9000

* `stat_contour()`, `stat_density2d()`, `stat_bin2d()`, `stat_binhex()`
now calculate normalized statistics including `nlevel`, `ndensity`, and
`ncount`. Also, `stat_density()` now includes the calculated statistic
`nlevel`, an aliasfor `scaled`, to better match the syntax of `stat_bin()`
(@bjreisman, #2679).

* `geom_hex()` now understands the `size` and `linetype` aesthetics
(@mikmart, #2488).

Expand Down
9 changes: 9 additions & 0 deletions R/geom-density2d.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
#' # set of contours for each value of that variable
#' d + geom_density_2d(aes(colour = cut))
#'
#' # Similarly, if you apply faceting to the plot, contours will be
#' # drawn for each facet, but the levels will calculated across all facets
#' d + stat_density_2d(aes(fill = stat(level)), geom = "polygon") +
#' facet_grid(. ~ cut) + scale_fill_viridis_c()
#' # To override this behavior (for instace, to better visualize the density
#' # within each facet), use stat(nlevel)
#' d + stat_density_2d(aes(fill = stat(nlevel)), geom = "polygon") +
#' facet_grid(. ~ cut) + scale_fill_viridis_c()
#'
#' # If we turn contouring off, we can use use geoms like tiles:
#' d + stat_density_2d(geom = "raster", aes(fill = stat(density)), contour = FALSE)
#' # Or points:
Expand Down
9 changes: 9 additions & 0 deletions R/stat-bin2d.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#' @param drop if `TRUE` removes all cells with 0 counts.
#' @export
#' @rdname geom_bin2d
#' @section Computed variables:
#' \describe{
#' \item{count}{number of points in bin}
#' \item{density}{density of points in bin, scaled to integrate to 1}
#' \item{ncount}{count, scaled to maximum of 1}
#' \item{ndensity}{density, scaled to maximum of 1}
#' }
stat_bin_2d <- function(mapping = NULL, data = NULL,
geom = "tile", position = "identity",
...,
Expand Down Expand Up @@ -74,7 +81,9 @@ StatBin2d <- ggproto("StatBin2d", Stat,
out$height <- ydim$length

out$count <- out$value
out$ncount <- out$count / max(out$count, na.rm = TRUE)
out$density <- out$count / sum(out$count, na.rm = TRUE)
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
out
}
)
Expand Down
9 changes: 9 additions & 0 deletions R/stat-binhex.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#' @export
#' @rdname geom_hex
#' @inheritParams stat_bin_2d
#' @section Computed variables:
#' \describe{
#' \item{count}{number of points in bin}
#' \item{density}{density of points in bin, scaled to integrate to 1}
#' \item{ncount}{count, scaled to maximum of 1}
#' \item{ndensity}{density, scaled to maximum of 1}
#' }
stat_bin_hex <- function(mapping = NULL, data = NULL,
geom = "hex", position = "identity",
...,
Expand Down Expand Up @@ -48,7 +55,9 @@ StatBinhex <- ggproto("StatBinhex", Stat,
wt <- data$weight %||% rep(1L, nrow(data))
out <- hexBinSummarise(data$x, data$y, wt, binwidth, sum)
out$density <- as.vector(out$value / sum(out$value, na.rm = TRUE))
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
out$count <- out$value
out$ncount <- out$count / max(out$count, na.rm = TRUE)
out$value <- NULL

out
Expand Down
3 changes: 3 additions & 0 deletions R/stat-contour.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' @section Computed variables:
#' \describe{
#' \item{level}{height of contour}
#' \item{nlevel}{height of contour, scaled to maximum of 1}
#' \item{piece}{contour piece (an integer)}
#' }
#' @rdname geom_contour
stat_contour <- function(mapping = NULL, data = NULL,
Expand Down Expand Up @@ -91,6 +93,7 @@ contour_lines <- function(data, breaks, complete = FALSE) {

data.frame(
level = rep(levels, lengths),
nlevel = rep(levels, lengths) / max(rep(levels, lengths), na.rm = TRUE),
x = xs,
y = ys,
piece = pieces,
Expand Down
9 changes: 8 additions & 1 deletion R/stat-density-2d.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#' using [MASS::bandwidth.nrd()].
#' @section Computed variables:
#' Same as [stat_contour()]
#'
#' With the addition of:
#' \describe{
#' \item{density}{the density estimate}
#' \item{ndensity}{density estimate, scaled to maximum of 1}
#' }
stat_density_2d <- function(mapping = NULL, data = NULL,
geom = "density_2d", position = "identity",
...,
Expand Down Expand Up @@ -63,9 +69,10 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
df$group <- data$group[1]

if (contour) {
StatContour$compute_panel(df, scales, bins, binwidth)
StatContour$compute_panel(df, scales, bins, binwidth)
} else {
names(df) <- c("x", "y", "density", "group")
df$ndensity <- df$density / max(df$density, na.rm = TRUE)
df$level <- 1
df$piece <- 1
df
Expand Down
4 changes: 4 additions & 0 deletions R/stat-density.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#' \item{count}{density * number of points - useful for stacked density
#' plots}
#' \item{scaled}{density estimate, scaled to maximum of 1}
#' \item{ndensity}{alias for `scaled`, to mirror the syntax of
#' [`stat_bin()`]}
#' }
#' @export
#' @rdname geom_density
Expand Down Expand Up @@ -92,6 +94,7 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1,
x = NA_real_,
density = NA_real_,
scaled = NA_real_,
ndensity = NA_real_,
count = NA_real_,
n = NA_integer_
))
Expand All @@ -104,6 +107,7 @@ compute_density <- function(x, w, from, to, bw = "nrd0", adjust = 1,
x = dens$x,
density = dens$y,
scaled = dens$y / max(dens$y, na.rm = TRUE),
ndensity = dens$y / max(dens$y, na.rm = TRUE),
count = dens$y * nx,
n = nx
)
Expand Down
10 changes: 10 additions & 0 deletions man/geom_bin2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/geom_contour.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/geom_density.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/geom_density_2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/geom_hex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 39 additions & 10 deletions man/ggtheme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/scale_viridis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-stat-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ test_that("compute_density returns useful df and throws warning when <2 values",
expect_warning(dens <- compute_density(1, NULL, from = 0, to = 0))

expect_equal(nrow(dens), 1)
expect_equal(names(dens), c("x", "density", "scaled", "count", "n"))
expect_equal(names(dens), c("x", "density", "scaled", "ndensity", "count", "n"))
expect_type(dens$x, "double")
})