Status Quo
library(tidyverse)
compute_line_end <- function(data, scales){
data |>
filter(x == max(x))
}
gapminder::gapminder |>
filter(continent == "Oceania") |>
rename(x = year, y = gdpPercap, color = country, label = country) |>
compute_line_end()
## # A tibble: 2 × 6
## label continent x lifeExp pop y
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Australia Oceania 2007 81.2 20434176 34435.
## 2 New Zealand Oceania 2007 80.2 4115771 25185.
StatLineend <- ggproto(`_class` = "StatLineend",
`_inherit` = Stat,
compute_group = compute_line_end,
)
gapminder::gapminder |>
filter(continent == "Oceania") |>
ggplot() +
aes(x = year, y = gdpPercap, color = country, label = country) +
geom_line(show.legend = F,) +
layer("point", "lineend", position = "identity" , show.legend = F) +
layer("text", "lineend", position = "identity", show.legend = F,
params = list(hjust = 1, vjust = 0, color = "black"))
last_plot() +
layer("label", "lineend", position = "identity")
## Warning in rep(params$label.padding, length.out = 4): 'x' is NULL so the result
## will be NULL
## Error in check.length(gparname): 'gpar' element 'lwd' must not be length 0