geom_text_hdx | R Documentation |
Text geoms are useful for labeling plots. They can be used by themselves as
scatterplots or in combination with other geoms, for example, for labeling
points or for annotating the height of bars. geom_text_hdx()
adds only
text to the plot. geom_label_hdx()
draws a rectangle behind the text,
making it easier to read. The only difference with the base geom_text()
is that the default font family is Source Sans 3. geom_label_hdx()
also
incorporates a default dark gray background, white text, and no borders.
geom_text_hdx(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_label_hdx(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
fill = hdx_hex("gray-dark"),
color = "white",
fontface = "bold",
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"),
label.size = 0,
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 A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer.
Cannot be jointy specified with
|
... |
Other arguments passed on to
|
parse |
If |
nudge_x , nudge_y |
Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with |
check_overlap |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
fill |
Fill color for label box. Defaults to dark gray. |
color |
Font color. Defaults to white. |
fontface |
Font emphasis. Defaults to bold. |
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. |
Note that when you resize a plot, text labels stay the same size, even though the size of the plot area changes. This happens because the "width" and "height" of a text element are 0. Obviously, text labels do have height and width, but they are physical units, not data units. For the same reason, stacking and dodging text will not work by default, and axis limits are not automatically expanded to include all text.
geom_text()
and geom_label()
add labels for each row in the
data, even if coordinates x, y are set to single values in the call
to geom_label()
or geom_text()
.
To add labels at specified points use annotate()
with
annotate(geom = "text", ...)
or annotate(geom = "label", ...)
.
To automatically position non-overlapping text labels see the ggrepel package.
A ggplot2 layer that can be added to a ggplot2::ggplot()
plot.
library(ggplot2)
load_source_sans_3()
p <- ggplot(
data = mtcars,
mapping = aes(
x = mpg,
y = mpg,
label = rownames(mtcars)
)
)
p + geom_text_hdx()
p + geom_label_hdx()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.