0

I wish to fill out ggrepel::geom_label_repel such that it works with sf-objects.

In order to do this, I need to be able to provide aes arguments and ... as well.

geom_sf_label_repel <- function(.data,
                                label,
                                alpha,
                                angle,
                                color,
                                family,
                                fontface,
                                group,
                                hjust,
                                lineheight,
                                size,
                                vjust, sf_column_name = "geometry", ...) {
  geometry <- pull(.data, sf_column_name)
  coordinates <- sf::st_coordinates(geometry)
  colnames(coordinates) <- c('x', 'y')
  coords_data <- dplyr::bind_cols(.data, coordinates)
  ggrepel::geom_label_repel(aes(x, y,
                                label = {{label}},
                                alpha = {{alpha}},
                                angle = {{angle}},
                                color = {{color}},
                                family = {{family}},
                                fontface = {{fontface}},
                                group = {{group}},
                                hjust = {{hjust}},
                                lineheight = {{lineheight}},
                                size = {{size}},
                                vjust = {{vjust}}
  ),
  ...,
  data = coords_data)
}

This seems excessively hacky and error-prone. Is there a right way to do this?

Mossa
  • 1,656
  • 12
  • 16

0 Answers0