| geom_hex {ggplot2} | R Documentation |
Hexagonal heatmap of 2d bin counts
Description
Divides the plane into regular hexagons, counts the number of cases in
each hexagon, and then (by default) maps the number of cases to the hexagon
fill. Hexagon bins avoid the visual artefacts sometimes generated by
the very regular alignment of geom_bin_2d().
Usage
geom_hex(
mapping = NULL,
data = NULL,
stat = "binhex",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
stat_bin_hex(
mapping = NULL,
data = NULL,
geom = "hex",
position = "identity",
...,
binwidth = NULL,
bins = 30,
na.rm = FALSE,
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 |
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
... |
Other arguments passed on to
|
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 |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom, stat |
Override the default connection between |
binwidth |
The width of the bins. Can be specified as a numeric value
or as a function that takes x after scale transformation as input and
returns a single numeric value. When specifying a function along with a
grouping structure, the function will be called once per group.
The default is to use the number of bins in The bin width of a date variable is the number of days in each time; the bin width of a time variable is the number of seconds. |
bins |
Number of bins. Overridden by |
Computed variables
These are calculated by the 'stat' part of layers and can be accessed with delayed evaluation.
-
after_stat(count)
number of points in bin. -
after_stat(density)
density of points in bin, scaled to integrate to 1. -
after_stat(ncount)
count, scaled to maximum of 1. -
after_stat(ndensity)
density, scaled to maximum of 1.
Controlling binning parameters for the x and y directions
The arguments bins and binwidth can
be set separately for the x and y directions. When given as a scalar, one
value applies to both directions. When given as a vector of length two,
the first is applied to the x direction and the second to the y direction.
Alternatively, these can be a named list containing x and y elements,
for example list(x = 10, y = 20).
Aesthetics
geom_hex() understands the following aesthetics. Required aesthetics are displayed in bold and defaults are displayed for optional aesthetics:
| • | x | |
| • | y | |
| • | alpha | → NA |
| • | colour | → via theme() |
| • | fill | → via theme() |
| • | group | → inferred |
| • | linetype | → via theme() |
| • | linewidth | → via theme()
|
stat_binhex() understands the following aesthetics. Required aesthetics are displayed in bold and defaults are displayed for optional aesthetics:
| • | x | |
| • | y | |
| • | fill | → after_stat(count) |
| • | group | → inferred |
| • | weight | → 1
|
Learn more about setting these aesthetics in vignette("ggplot2-specs").
See Also
stat_bin_2d() for rectangular binning
Examples
d <- ggplot(diamonds, aes(carat, price))
d + geom_hex()
# You can control the size of the bins by specifying the number of
# bins in each direction:
d + geom_hex(bins = 10)
d + geom_hex(bins = 30)
# Or by specifying the width of the bins
d + geom_hex(binwidth = c(1, 1000))
d + geom_hex(binwidth = c(.1, 500))