lons = seq(-180, 180, by = 30) lats = seq(-90, 90, by = 30) grat = st_graticule(lon = lons, lat = lats) box = st_bbox(c(xmin = -180, xmax = 180, ymax = 90, ymin = -90), crs = st_crs(4326)) |> st_as_sfc() |> smoothr::densify(max_distance = 1) degree_labels = function(grat, vjust, hjust, size, lon = T, lat = T) { pts = grat |> st_cast('POINT') |> group_by(degree, type, degree_label) |> filter(row_number() == 1) list( if (lon) geom_sf_text(data = filter(pts, type == 'E'), vjust = vjust, size = size, mapping = aes(label = degree_label), parse = TRUE), if (lat) geom_sf_text(data = filter(pts, type == 'N'), hjust = hjust, size = size, mapping = aes(label = degree_label), parse = TRUE) ) }