3

I'm trying to create a map with ggplot where sites are indicated using a point and a number on the map, and that numbering scheme is retained in the legend. So I'd like the first site to be labeled by a point with the number 1 in the center, and so on. Here's my code now:

library(tidyverse)

# download site data

dod <- read_csv('https://raw.githubusercontent.com/jaymwin/fcpp_GIS/master/data/DoD_sites_latlong.csv') %>%
  filter(site != 'Fort Wainwright') %>%
  mutate(
    num = row_number() # my way of numbering the sites
  )
dod

# download state outlines

states <- map_data('state')

# plot them

ggplot() +
  geom_polygon(data = states, aes(long, lat, group = group), fill = 'grey95', color = 'darkgrey') +
  geom_point(data = dod, aes(x = lon, y = lat, color = site), size = 4) +
  geom_text(data = dod, aes(label = num, x = lon, y = lat), 
            size = 2, color = 'white', fontface = 'bold') +
  scale_color_manual(values = c(rep('tomato', 12)), name = 'Site') +
  labs(
    x = 'Longitude',
    y = 'Latitude'
  ) +
  theme_minimal()

here's what the map looks like now:

Is it possible to have the map numbering scheme to correspond to the legend numbering scheme?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Jason
  • 892
  • 1
  • 8
  • 19
  • This seems like a lot of work, but should do the trick: https://stackoverflow.com/questions/49965758/change-geom-texts-default-a-legend-to-label-string-itself – aaumai Jul 14 '19 at 15:12
  • 1
    This would be easier if you use letters instead of numbers, because `scale_shape_manual` accepts single-character letters / numbers as symbols, but you'd be limited to 9 numerical symbols (10 if you count from 0), while letters allow up to 26. Grob hackings (as per link) above is another way to do it, if you really want numbers. – Z.Lin Jul 22 '19 at 08:05
  • 1
    p.s. You may want to check the mapping between row number and the order of sites in your legend. I don't think the two are quite the same. – Z.Lin Jul 22 '19 at 08:05
  • Looks like it is solved here: https://stackoverflow.com/questions/24801987/numbered-point-labels-plus-a-legend-in-a-scatterplot – Dima Lituiev Mar 24 '23 at 20:13

0 Answers0