geom_label_npc {ggpp} | R Documentation |
Text with Normalised Parent Coordinates
Description
geom_text_npc()
adds text directly to the plot.
geom_label_npc()
draws a rectangle behind the text, making it easier
to read. The difference is that x
and y
mappings are expected
to be given in npc
graphic units, using pseudo-aesthetics. Their
intended use is to add annotations to a plot.
Usage
geom_label_npc(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
label.padding = grid::unit(0.25, "lines"),
label.r = grid::unit(0.15, "lines"),
label.size = 0.25,
size.unit = "mm",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = FALSE
)
geom_text_npc(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = FALSE
)
Arguments
mapping |
The aesthetic mapping, usually constructed with
|
data |
A layer specific data set - only needed if you want to override the plot defaults. |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
other arguments passed on to |
parse |
If TRUE, the labels will be parsed into expressions and
displayed as described in |
nudge_x , nudge_y |
Horizontal and vertical adjustment to nudge labels by. Useful for offsetting text from points, particularly on discrete scales. |
label.padding |
Amount of padding around label. Defaults to 0.25 lines. |
label.r |
Radius of rounded corners. Defaults to 0.15 lines. |
label.size |
Size of label border, in mm. |
size.unit |
How the 'size' aesthetic is interpreted: as millimetres ('"mm"', default), points ('"pt"'), centimetres ('"cm"'), inches ('"in"'), or picas ('"pc"'). |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
check_overlap |
If 'TRUE', text that overlaps previous text in the same layer will not be plotted. |
Details
These geoms are identical to 'ggplot2'
geom_text
and geom_label
except that they interpret npcx
and npcy
positions in
npc
units. They translate npcx
and npcy
coordinates
using a pseudo-aesthetic with a fixed scale, the translation is done
separately for each plot panel. All aesthetics other than x and
y and grouping work as in normal geoms. These include
linetype
and angle
in geom_label_npc()
.
Alignment
With textual positions and groups a shift is added to successive labels to avoid overlaps. The shift is based on grouping, however unused levels are not dropped. In plots with faceting, if not all groups appear in each panel, there will be blank spaces in between labels. To solve this pass numeric values for the npc coordinates of each label instead of character strings.
You can modify text alignment with the vjust
and
hjust
aesthetics. These can either be a number between 0
(right/bottom) and 1 (top/left) or a character ("left"
,
"middle"
, "right"
, "bottom"
, "center"
,
"top"
). In addition, you can use special alignments for
justification including "position"
, "inward"
and
"outward"
. Inward always aligns text towards the center of the
plotting area, and outward aligns it away from the center of the plotting
area. If tagged with _mean
or _median
(e.g.,
"outward_mean"
) the mean or median of the data in the panel along
the corresponding axis is used as center. If the characters following the
underscore represent a number (e.g., "outward_10.5"
) the reference
point will be this value in data units. Position justification is computed
based on the direction of the displacement of the position of the label so
that each individual text or label is justified outwards from its original
position. The default justification is "position"
.
If no position displacement is applied, or a position function defined in
'ggplot2' is used, these geometries behave similarly to the corresponding
ones from package 'ggplot2' with a default justification of 0.5
and
no segment drawn.
Plot boundaries and clipping
Note that when you change the scale
limits for x and/or y of a plot, text labels stay the same
size, as determined by the size
aesthetic, given in millimetres. The
actual size as seen in the plotted output is decided during the rendering
of the plot to a graphics device. Limits are expanded only to include the
anchor point of the labels because the "width" and "height" of a text
element are 0 (as seen by ggplot2). Text labels do have height and width,
but in grid units, not data units.
See Also
geom_text
and
geom_label
for additional details.
Examples
df <- data.frame(
x = c(0, 0, 1, 1, 0.5),
x.chr = c("left", "left", "right", "right", "center"),
y = c(0, 1, 0, 1, 0.5),
y.chr = c("bottom", "top", "bottom", "top", "middle"),
text = c("bottom-left", "top-left", "bottom-right", "top-right", "center-middle")
)
ggplot(df) +
geom_text_npc(aes(npcx = x, npcy = y, label = text))
ggplot(df) +
geom_text_npc(aes(npcx = x.chr, npcy = y.chr, label = text))
ggplot(df) +
geom_text_npc(aes(npcx = x.chr, npcy = y.chr, label = text),
angle = 90)
ggplot(data = mtcars, mapping = aes(wt, mpg)) +
geom_point() +
geom_text_npc(data = df, aes(npcx = x, npcy = y, label = text))
ggplot(data = mtcars, mapping = aes(wt, mpg)) +
geom_point() +
geom_text_npc(data = df, aes(npcx = x, npcy = y, label = text)) +
expand_limits(y = 40, x = 6)
ggplot(data = mtcars) +
geom_point(mapping = aes(wt, mpg)) +
geom_label_npc(data = df, aes(npcx = x, npcy = y, label = text))
ggplot(data = mtcars) +
geom_point(mapping = aes(wt, mpg)) +
geom_label_npc(data = df, aes(npcx = x.chr, npcy = y.chr, label = text),
angle = 90) # ignored by ggplot2 < 3.5.0