0
votes

So, I have created a map of the contiguous USA using ggplot and then made a separate graph(/map) of specific data points within the USA, based upon their long/lat position and I am wondering how one may overlay these specific data points on to the map. Code as follows:

america_map <- map_data("world", region='USA')
USA_map <-
  ggplot(america_map, aes(x=long, y=lat, group=group)) +
  geom_polygon() + 
  scale_x_continuous(limits = c(-125,-65)) +
  scale_y_continuous(limits = c(25, 50)) + 
  coord_map() +
USA_map

Ravg_map <-
  ggplot(longlat_LH, aes(x=longitude, y=latitude))+
  geom_point(aes(color=Ravg))
Ravg_map

The longlat_LH df looks like this for reference:

> head(longlat_LH)
 X latitude longitude site_id Ravg
 1    38.42   -108.38     409 40.8
 2    40.17   -107.06     426 40.3
 3    37.79   -108.02     465 37.0
 4    39.06   -108.06     622 43.6
 5    37.75   -107.69     632 41.3
 9    37.65   -108.01     739 39.7
1
USA_map + geom_point(data = longlat_LH, aes(x=longitude, y=latitude, color = Ravg)) - Gregor Thomas

1 Answers

1
votes

Add your geoms directly to USA_map, but set the data= argument to longlat_LH. Here's an example that uses the ggrepel package to space out labels that are close to one another and avoid overlapping:

library(ggrepel)

USA_map +
  geom_point(data=longlat_LH,
    mapping=aes(x=longitude, y=latitude, group=NULL),
    color='white'
  ) +
  geom_text_repel(data=longlat_LH,
    mapping=aes(x=longitude, y=latitude, group=1, label=site_id),
    color='white'
  )

enter image description here