Is it possible to use ggplot2 / ggmap to plot geom_points randomly within a given spatial area defined by a shapefile?
I considered geom_jitter, however I need the plots to be randomly distributed and not cross spatial borders.
Sample data shamelessly borrowed from @matthiash here.
library(rgdal)
library(ggmap)
# Get shapefile with Drammen municipality borders
tmpzip<-tempfile()
tmpdir<-tempfile()
dir.create(tmpdir)
download.file("http://www.kartverket.no/Documents/Kart/N50-N5000%20Kartdata/33_N5000_shape.zip",tmpzip)
unzip(tmpzip, exdir=tmpdir)
kommune <- readOGR(dsn=tmpdir, layer="NO_AdminOmrader_pol")
kommune<-kommune[kommune$NAVN=="Drammen",]
kommune<-spTransform(kommune, CRS("+init=epsg:4326"))
dat<-fortify(kommune)
#get the base map
map <- get_map(location = "Drammen",
maptype = "watercolor", source = "stamen", zoom = 11)
Below code plots the base map with region id 154 from the shapefile plotted on top.
ggmap(map, extent = "normal", maprange = TRUE)+
geom_polygon(data = dat,
aes(long, lat, group = group),
fill = "orange", colour = "red", alpha = 0.2)
What I'd like to do is plot 10 points randomly within the shapefile region defined by dat$id==154