Search code examples
rggplot2mapslatitude-longitude

How to put geocoordinates from a dataframe on a map in R?


I would like to draw points and text labels for multiple locations defined by longitude and latitude. It works fine for a single location, but I struggle to extend this in geom_point for a group of data.

library(ggplot2)
require(maps)

GER <- map_data("world", region = "Germany")

ggplot(GER, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightgray", colour = "white")+
  geom_point(aes(x = 13.404954, y = 52.520008), color="red")+
  geom_text(aes(x = 13.404954, y = 52.520008), label = "Berlin", color = "red", nudge_y = .2) 

df <- data.frame(name = c("Berlin", "Hamburg"), 
                 long = c(13.404954, 9.993682),
                 lat = c(52.520008, 53.551086))

# How to do it?
ggplot(GER, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightgray", colour = "white")+
  geom_point(aes(x = df$long, y = df$lat), color="red")

enter image description here


Solution

  • Two different data sets have to be made explicit

    ggplot(data = GER, aes(x = long, y = lat, group=group)) +
      geom_polygon(fill="lightgray", colour = "white")+
      geom_point(data = df, aes(x = long, y = lat, group=name), color="red")
    

    enter image description here