Perform a 2D kernel density estimation using MASS::kde2d() and display the results with contours. This can be useful for dealing with overplotting. This is a 2D version of geom_density(). geom_density_2d() draws contour lines, and geom_density_2d_filled() draws filled contour bands.
geom_density_2d({
"mapping" = null,
"data" = null,
"stat" = "density_2d",
"position" = "identity",
"...",
"contour_var" = "density",
"lineend" = "butt",
"linejoin" = "round",
"linemitre" = 10,
"na.rm" = false,
"show.legend" = NA,
"inherit.aes" = true
})
geom_density_2d_filled({
"mapping" = null,
"data" = null,
"stat" = "density_2d_filled",
"position" = "identity",
"...",
"contour_var" = "density",
"na.rm" = false,
"show.legend" = NA,
"inherit.aes" = true
})
stat_density_2d({
"mapping" = null,
"data" = null,
"geom" = "density_2d",
"position" = "identity",
"...",
"contour" = true,
"contour_var" = "density",
n ="100",
h ="NULL",
adjust = c({1, 1}),
"na.rm" = false,
"show.legend" = NA,
"inherit.aes" = true
})
stat_density_2d_filled({
"mapping" = null,
"data" = null,
"geom" = "density_2d_filled",
"position" = "identity",
"...",
"contour" = true,
"contour_var" = "density",
n ="100",
h ="NULL",
adjust = c({1, 1}),
"na.rm" = false,
"show.legend" = NA,
"inherit.aes" = true
})
mapping |
Set of aesthetic mappings created by | |
---|---|---|
data |
The data to be displayed in this layer. There are three options: If NULL, the default, the data is inherited from the plot data as specified in the call to A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created. A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)). | |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. | |
... |
\Arguments passed on to geom_contour | |
bins |
Number of contour bins. Overridden by binwidth. | |
binwidth |
The width of the contour bins. Overridden by breaks. | |
breaks |
Numeric vector to set the contour breaks. Overrides binwidth and bins. By default, this is a vector of length ten with pretty() breaks. | |
contour_var |
Character string identifying the variable to contour by. Can be one of "density", "ndensity", or "count". See the section on computed variables for details. | |
lineend |
Line end style (round, butt, square). | |
linejoin |
Line join style (round, mitre, bevel). | |
linemitre |
Line mitre limit (number greater than 1). | |
na.rm |
If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed. | |
show.legend |
logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display. | |
inherit.aes |
If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders(). | |
geom, stat |
Use to override the default connection between | |
contour |
If TRUE, contour the results of the 2d density estimation. | |
n |
Number of grid points in each direction. | |
h |
Bandwidth (vector of length two). If NULL, estimated using MASS::bandwidth.nrd(). | |
adjust |
A multiplicative bandwidth adjustment to be used if 'h' is 'NULL'. This makes it possible to adjust the bandwidth while still using the a bandwidth estimator. For example, adjust = 1/2 means use half of the default bandwidth. |
geom_density_2d() understands the following aesthetics (required aesthetics are in bold)
:
x
y
alpha
color
group
linetype
size
Learn more about setting these aesthetics in vignette("cxplot-specs").
geom_density_2d_filled() understands the following aesthetics (required aesthetics are in bold)
:
x
y
alpha
color
fill
group
linetype
size
subgroup
Learn more about setting these aesthetics in vignette("cxplot-specs").
stat_density_2d() and stat_density_2d_filled() compute different variables depending on whether contouring is turned on or off. With contouring off (contour = FALSE), both stats behave the same, and the following variables are provided:
density
Density estimate, scaled to a maximum of 1.
count
Number of observations in each group.
With contouring on (contour = TRUE), either stat_contour() or stat_contour_filled() (for contour lines or contour bands, respectively) is run after the density estimate has been obtained, and the computed variables are determined by these stats. Contours are calculated for one of the three types of density estimates obtained before contouring, density, ndensity, and count. Which of those should be used is determined by the contour_var parameter.
var cxp = new cxplot("canvas1", faithful, aes({"x": "eruptions", "y": "waiting"}));
cxp.geom_point();
cxp.xlim(0.5, 6);
cxp.ylim(40, 110);
// contour lines
cxp.geom_density_2d();
// contour bands
var cxp = new cxplot("canvas2", faithful, aes({"x": "eruptions", "y": "waiting"}));
cxp.geom_point();
cxp.xlim(0.5, 6);
cxp.ylim(40, 110);
cxp.geom_density_2d_filled({"alpha" : 0.5});
// From R:
// set.seed(4393)
// dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
var cxp = new cxplot("canvas3", dsmall, aes("x", "y"));
// If you map an aesthetic to a categorical variable, you will get a
// set of contours for each value of that variable
cxp.geom_density_2d(aes({"color": "cut"}));
// If you draw filled contours across multiple facets, the same bins are
// used across all facets
var cxp = new cxplot("canvas4", dsmall, aes("x", "y"));
cxp.geom_density_2d_filled();
cxp.facet_wrap("cut")
// If you want to make sure the peak intensity is the same in each facet,
// use `contour_var = "ndensity"`.
var cxp = new cxplot("canvas5", dsmall, aes("x", "y"));
cxp.geom_density_2d_filled({"contour_var": "ndensity"});
cxp.facet_wrap("cut");
// If you want to scale intensity by the number of observations in each group,
// use `contour_var = "count"`.
var cxp = new cxplot("canvas6", dsmall, aes("x", "y"));
cxp.geom_density_2d_filled({"contour_var": "count"});
cxp.facet_wrap("cut");
// If we turn contouring off, we can use other geoms, such as tiles:
var cxp = new cxplot("canvas7", dsmall, aes("x", "y"));
cxp.geom_raster();