Just in case it wasn't obvious (as it wasn't for me), we can add the GeomShadowText
from {shadowtext}
to the function `geom_sf_text()` from {sf}
in place of the existing geom = GeomText
argument.
geom_sf_shadowtext <- function(
mapping = aes(),
data = NULL,
stat = "sf_coordinates",
position = "identity",
...,
parse = FALSE,
nudge_x = 0,
nudge_y = 0,
check_overlap = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
fun.geometry = NULL
) {
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position)) {
cli::cli_abort(c(
"Both {.arg position} and {.arg nudge_x}/{.arg nudge_y} are supplied.",
i = "Only use one approach to alter the position."
))
}
position <- position_nudge(nudge_x, nudge_y)
}
layer_sf(
data = data,
mapping = mapping,
stat = stat,
geom = GeomShadowText,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
parse = parse,
check_overlap = check_overlap,
na.rm = na.rm,
fun.geometry = fun.geometry,
...
)
)
}
I couldn't easily get the example in the original question to work due to API key issues, so here's a simpler working example:
library(ggplot2)
library(sf)
library(shadowtext)
library(rnaturalearth)
Africa <- ne_countries(continent = "Africa")
ggplot(data = Africa) +
geom_sf() +
geom_sf_shadowtext(mapping = aes(label = name_en))