Using gg repel to add labels to map

121 Views Asked by At

I'm using ggplot to draw a map of a specific area and plot some sites, and I am having trouble using ggrepel to avoid the labels form overlapping.

I use ggplot2 to map the area of interest, geom_point() to plot the sites, and geom_label() to add the labels. When the labels are plotted, they are all overlapping (the sites are close together). When I added a ggrepel line, the plot was not printed and I received

Error: Aesthetics must be either length 1 or the same as the data (241): x, y and label

and I'm lost for what to do to fix it. Any help is appreciated, thanks!

My dataset is

# A tibble: 18 x 3
   `sample #` `GPS lat` `GPS lon`
   <chr>          <dbl>     <dbl>
 1 ARG18-025      -45.5     -67.3
 2 ARG18-026      -45.6     -67.3
 3 ARG18-031      -45.6     -67.3
 4 ARG18-033      -45.6     -67.3
 5 ARG18-044      -45.5     -67.3
 6 ARG18-048      -45.5     -67.2
 7 ARG18-049      -45.5     -67.2
 8 ARG18-050      -45.5     -67.2
 9 ARG18-051      -45.5     -67.2
10 ARG18-052      -45.5     -67.2
11 ARG18-056      -45.5     -67.2
12 ARG18-057      -45.5     -67.2
13 ARG18-059      -45.5     -67.2
14 ARG18-068      -45.0     -66.9
15 ARG18-071      -45.0     -66.9
16 ARG18-072      -45.0     -66.9
17 ARG18-073      -45.0     -66.9
18 ARG18-074      -45.0     -66.9

and my code is

ggplot(data = world) +
  geom_sf() +
  geom_point(
    data = sites, aes(x = sites$`GPS lon`, y = sites$`GPS lat`), size = 4,
    shape = 20, fill = "black"
  ) +
  coord_sf(
    xlim = c(-68, -65),
    ylim = c(-44.5, -46), expand = FALSE
  ) +
  geom_label(data = sites, aes(
    x = sites$`GPS lon`, y = sites$`GPS lat`,
    label = sites$`sample #`
  ), size = 5, fontface = "bold") +
  geom_text_repel(
    aes(
      x = sites$`GPS lon`,
      y = sites$`GPS lat`,
      label = sites$`sample #`
    )
  )
0

There are 0 best solutions below