Contour lines representing 2D density are available already in
ggplot2, but the native geom_density_2d
does not allow the lines to be labelled with the level of each contour.
geom_textdensity2d
adds this ability.
geom_textdensity2d(
mapping = NULL,
data = NULL,
stat = "density_2d",
position = "identity",
...,
contour_var = "density",
n = 100,
h = NULL,
adjust = c(1, 1),
lineend = "butt",
linejoin = "round",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)geom_labeldensity2d(
mapping = NULL,
data = NULL,
stat = "density_2d",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...,
contour_var = "density",
n = 100,
h = NULL,
adjust = c(1, 1),
lineend = "butt",
linejoin = "round",
linemitre = 10,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
arrow = NULL
)
A Layer
ggproto object that can be added to a plot.
Set of aesthetic mappings created by aes()
. If specified and
inherit.aes = TRUE
(the default), it is combined with the default mapping
at the top level of the plot. You must supply mapping
if there is no plot
mapping.
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 ggplot()
.
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)
).
The statistical transformation to use on the data for this
layer, either as a ggproto
Geom
subclass or as a string naming the
stat stripped of the stat_
prefix (e.g. "count"
rather than
"stat_count"
)
Position adjustment, either as a string naming the adjustment
(e.g. "jitter"
to use position_jitter
), or the result of a call to a
position adjustment function. Use the latter if you need to change the
settings of the adjustment.
Other arguments passed on to layer
. These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red"
or size = 3
. These can also be the following text-path parameters:
gap
A logical(1)
which if TRUE
, breaks the path into two sections with a gap on either side of the label. If FALSE
, the path is plotted as a whole. Alternatively, if NA
, the path will be broken if the string has a vjust
between 0 and 1, and not otherwise. The default for the label variant is FALSE
and for the text variant is NA
.
upright
A logical(1)
which if TRUE
(default), inverts any text where the majority of letters would upside down along the path, to improve legibility. If FALSE
, the path decides the orientation of text.
halign
A character(1)
describing how multi-line text should be justified. Can either be "center"
(default), "left"
or "right"
.
offset
A unit
object of length 1 to determine the offset of the text from the path. If this is NULL
(default), the vjust
parameter decides the offset. If not NULL
, the offset
argument overrules the vjust
setting.
parse
A logical(1)
which if TRUE
, will coerce the labels into expressions, allowing for plotmath syntax to be used.
straight
A logical(1)
which if TRUE
, keeps the letters of a label on a straight baseline and if FALSE
(default), lets individual letters follow the curve. This might be helpful for noisy paths.
padding
A unit
object of length 1 to determine the padding between the text and the path when the gap
parameter trims the path.
text_smoothing
a numeric(1)
value between 0 and 100 that smooths the text without affecting the line portion of the geom. The default value of 0
means no smoothing is applied.
rich
A logical(1)
whether to interpret the text as html/markdown formatted rich text. Default: FALSE
. See also the rich text section of the details in geom_textpath()
.
remove_long
if TRUE, labels that are longer than their associated path will be removed.
Character string identifying the variable to contour
by. Can be one of "density"
, "ndensity"
, or "count"
. See the section
on computed variables for details.
Number of grid points in each direction.
Bandwidth (vector of length two). If NULL
, estimated
using MASS::bandwidth.nrd()
.
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.
Line end style (round, butt, square).
Line join style (round, mitre, bevel).
Line mitre limit (number greater than 1).
If FALSE
, the default, missing values are removed with
a warning. If TRUE
, missing values are silently removed.
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.
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()
.
Amount of padding around label. Defaults to 0.25 lines.
Radius of rounded corners. Defaults to 0.15 lines.
Arrow specification, as created by grid::arrow()
.
geom_textdensity2d()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
angle
colour
family
fontface
group
hjust
linecolour
lineheight
linetype
linewidth
size
spacing
textcolour
vjust
In addition to aforementioned aesthetics, geom_labeldensity2d()
also understands:
boxcolour
boxlinetype
boxlinewidth
fill
The spacing
aesthetic allows fine control of spacing
of text, which is called 'tracking' in typography.
The default is 0 and units are measured in 1/1000 em.
Numbers greater than zero increase the spacing,
whereas negative numbers decrease the spacing.
Learn more about setting these aesthetics
in vignette("ggplot2-specs")
.
Other geom layers that place text on paths.
set.seed(1)
df <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(df, aes(x, y)) +
geom_textdensity2d() +
theme_classic()
Run the code above in your browser using DataLab