I am using ggplot2 in an oceanographic context, and I would like to add a simple map of the land for reference. Here's a minimum working example in basic graphics:
library(ggplot2)
library(maps)
#Setup fake data - some points in the North Sea
n <- 100
d <- data.frame(lon=rnorm(n,5),lat=rnorm(n,55))
#Plotting with base-graphics, then overlaying map
plot(lat~lon,d,pch=16,col="red",asp=1)
map("world",add=TRUE,col="black",fill=TRUE)
Note that the xlim and ylim of this plot are set by the range of the data.
Now, attempting to reproduce this in ggplot:
#And now with ggplot
g <- ggplot(d,aes(lon,lat))+geom_point(col="red",pch=16)+
borders("world",fill="black",colour="black")
plot(g)
...which is technically correct - the axes are set to cover all of the points - but is not really what I'm after.
I can of course go in and set the axes manually using e.g. coord_cartesian
or similar, but this has a high irritation factor - particularly when trying to produce lots of similar maps automatically. Ideally I would like to be able to overlay the borders/coastlines and tell ggplot not to consider them when determining the axes. Alternatively, I have the impression that geom_map
map be the way forward, but I'm not having much success with that..
Any suggestions?
Mark