PAC
PAC

Reputation: 5366

Is it possible to overlay a scatterplot on map using ggplot?

I would like to draw a map of French departements (ie districts in France) and plot points on the map. I use ggplot2 to draw the map but when I want to add points on the map, R returns an error which says that it cannot find 'group'.

library("ggplot2")
library("ggmap")

# load the contour of French departements
wg  <- read.csv("data/departements.csv")

metropoles  <- c("Paris", "Rennes", "Rouen", "Lille", "Marseille")
geo  <- geocode(metropoles)
met  <- data.frame(ville = metropoles, geo)

map  <- ggplot(data = wg, aes(x = long, y = lat, group = group)) + 
geom_polygon() + 
scale_x_continuous(limits = c(-7,10)) + 
scale_y_continuous(limits = c(40,53)) + 
coord_map() + 
theme(axis.text = element_blank(), axis.title = element_blank()) 

map + geom_point(data = met, aes(x = lon, y = lat))

For replication, you can find the raw data here and the R program here

Upvotes: 2

Views: 3472

Answers (1)

PAC
PAC

Reputation: 5366

@Roland had the good answer. The problem is that I had defined group as a general argument.

ggplot() + 
  geom_polygon(data = wg, aes(x = long, y = lat, group = group)) + 
  scale_x_continuous(limits = c(-7,10)) + 
  scale_y_continuous(limits = c(40,53)) + 
  coord_map() + 
  theme(axis.text = element_blank(), axis.title = element_blank()) 
  geom_point(data = met, aes(x = lon, y = lat))

Upvotes: 2

Related Questions