View source: R/geom_text_lastonly.R
geom_text_lastonly | R Documentation |
Label only the last point(s) on a plot. geom_text_lastonly()
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(
mapping = NULL,
data = NULL,
stat = "identity",
position = NULL,
parse = FALSE,
nudge_x = 0.25,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
add_points = FALSE,
text_aes = NULL,
point_aes = NULL,
...
)
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 |
Position adjustment, either as a string, or the result of a
call to a position adjustment function. Cannot be jointy specified with
|
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 jointy specified with |
check_overlap |
If |
na.rm |
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 ggplot2::geom_text()
and
ggplot2::geom_point()
.
df <- data.frame(year=2010:2020, value=runif(22), var=c(rep("A", 11), rep("B", 11)))
# 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.