-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement filled 2d density contours #3864
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
Implement filled 2d density contours #3864
Conversation
This comment has been minimized.
This comment has been minimized.
Never mind. Apparently this works only from inside a stat. library(tidyverse)
data(airquality)
airQ <- filter_all(airquality, ~!is.na(.))
ggplot(airQ, aes(x = Solar.R, y = Temp)) +
geom_density2d_filled(alpha = 0.7) +
geom_density2d(size = 0.25, color = "black") +
geom_point(size = 1) Created on 2020-03-07 by the reprex package (v0.3.0) |
@thomasp85 How's this? library(ggplot2)
ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
geom_contour_filled() +
facet_wrap(~waiting < 70) set.seed(4393)
dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsmall, aes(x, y))
d + geom_density_2d_filled() +
facet_wrap(~cut) d + geom_density_2d_filled(contour_var = "ndensity") +
facet_wrap(~cut) d + geom_density_2d_filled(contour_var = "count") +
facet_wrap(~cut) d + geom_density_2d(aes(color = after_stat(level))) +
facet_wrap(~cut) d + geom_density_2d(aes(color = after_stat(level)), contour_var = "ndensity") +
facet_wrap(~cut) d + geom_density_2d(aes(color = after_stat(level)), contour_var = "count") +
facet_wrap(~cut)
#> Warning: stat_contour(): Zero contours were generated
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf Created on 2020-03-10 by the reprex package (v0.3.0) |
This looks good. The It hurts a little that Some part of me really wants to make the contouring happen in the geom so that anything that ends up as a raster hightmap after the stat can be contoured... but then we end up with the issues of colour scaling the output of contouring –– no singular best solution it seems |
If the geom could get the height data from the stat and the binning information from the scale then it might work. We could always in the future add such geoms in addition to |
I think this is ready for review now. Closes #3846. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any difference between stat_bin2d
and the new stat_density_2d()
with contour = FALSE
. To me they seem superficially equivalent (at least in intend)
#' @usage NULL | ||
#' @export | ||
#' @include geom-polygon.r | ||
GeomContourFilled <- ggproto("GeomContourFilled", GeomPolygon) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this? Do we have any precedence for creating ggproto aliases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to auto-generate documentation for aesthetics. We probably should do it more consistently across ggplot2. See here:
https://github.com/clauswilke/ggplot2/blob/743bad010638fdccaed4c0cfa0258e0879e1263c/R/geom-density2d.r#L9-L10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... that seems bad, but is not really the fault of this PR
The relationship between |
You are right. I got bin2d confused |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Implement filled 2d density contours (tidyverse#3864)
The missing feature from 3.3.0, as described in #3846.
This is work in progress. Still to do:
geom_density2d_filled()
analogously togeom_contour_filled()
nlevel
work forgeom_density2d_filled()
.stat_contour_filled()