View source: R/geom_text_lastonly_repel.R
geom_text_lastonly_repel | R Documentation |
Label only the last point(s) on a plot. geom_text_lastonly_repel()
can be
used instead of ggplot2::geom_text()
when only the last point(s)
should be labeled. This is accomplished by identifying the maximum value of
x
in data
and applying a filter to omit records where x
is less than the maximum.
geom_text_lastonly_repel(
mapping = NULL,
data = NULL,
stat = "identity",
position = NULL,
parse = FALSE,
box.padding = 0.25,
point.padding = 1e-06,
min.segment.length = 0.5,
arrow = NULL,
force = 1,
force_pull = 1,
max.time = 0.5,
max.iter = 10000,
max.overlaps = getOption("ggrepel.max.overlaps", default = 10),
nudge_x = 0.4,
nudge_y = 0,
xlim = c(NA, NA),
ylim = c(NA, NA),
na.rm = FALSE,
check_overlap = FALSE,
direction = c("y", "x", "both"),
seed = NA,
verbose = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
add_points = FALSE,
text_aes = NULL,
point_aes = NULL,
...
)
mapping |
Set of aesthetic mappings created by |
data |
A data frame. If specified, overrides the default data frame defined at the top level of the plot. |
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. |
parse |
If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath |
box.padding |
Amount of padding around bounding box, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
point.padding |
Amount of padding around labeled point, as unit or
number. Defaults to 0. (Default unit is lines, but other units can be
specified by passing |
min.segment.length |
Skip drawing segments shorter than this, as unit or
number. Defaults to 0.5. (Default unit is lines, but other units can be
specified by passing |
arrow |
specification for arrow heads, as created by |
force |
Force of repulsion between overlapping text labels. Defaults to 1. |
force_pull |
Force of attraction between a text label and its corresponding data point. Defaults to 1. |
max.time |
Maximum number of seconds to try to resolve overlaps. Defaults to 0.5. |
max.iter |
Maximum number of iterations to try to resolve overlaps. Defaults to 10000. |
max.overlaps |
Exclude text labels when they overlap too many other things. For each text label, we count how many other text labels or other data points it overlaps, and exclude the text label if it has too many overlaps. Defaults to 10. |
nudge_x , nudge_y |
Horizontal and vertical adjustments to nudge the
starting position of each text label. The units for |
xlim , ylim |
Limits for the x and y axes. Text labels will be constrained to these limits. By default, text labels are constrained to the entire plot area. |
na.rm |
If |
direction |
"both", "x", or "y" – direction in which to adjust position of labels |
seed |
Random seed passed to |
verbose |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
add_points |
If |
text_aes , point_aes |
Named list, additional aesthetics to send to the text and point geoms, respectively. |
... |
Additional aesthetics to send to BOTH the point and text geoms.
Note that if |
Labels are placed by default to the right of the final point, and may be partially cut off by the plot limits. There are two known ways to address this:
Turn off panel clipping, e.g. with
coord_cartesian(clip = "off")
. Substitute the correct coordinate
system for your plot–all have a clip
argument available. Note that
this will allow all geoms in the plot to draw outside the panel area, which
may have unintended consequences.
Manually expand the x
scale,
e.g. with scale_x_continuous(expand=expand_scale(mult=0.10))
or
coord_cartesian(xlim = c(min, max))
.
Code was mostly copied from the source of ggrepel::geom_text_repel()
and
ggplot2::geom_point()
.
library(tidyverse)
df <- transit_ridership %>%
filter(system != "pace_ada") %>%
mutate(system = recode_factor(system,
cta_bus = "CTA bus",
cta_rail = "CTA rail",
metra = "Metra",
pace = "Pace"))
# Without points, label formatting or x-axis expansion
ggplot(df, aes(x=year, y=value, color=var)) +
geom_line() +
labs(title="Random lines") +
scale_y_continuous("Percentage of absolutely nothing") +
scale_x_continuous("Year") +
geom_text_lastonly()
# With points, label formatting and x-axis expansion
ggplot(df, aes(x=year, y=value, color=var, label=sprintf("%.1f%%", 100*value))) +
geom_line() +
labs(title="Random lines") +
scale_y_continuous("Percentage of absolutely nothing", labels=scales::percent) +
scale_x_continuous("Year", expand=expansion(mult=c(0.05, 0.10))) +
geom_text_lastonly(add_points=TRUE, text_aes=list(fontface="bold"), point_aes=list(size=2.5))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.