My dataset consist in a converted raster dataframe, with for each point a long/lat, a categorical value and a numerical value are associated with. It can be downloaded here.
The code hereafter allows me to generate this map.
My issue is that I can't find a way to get rid of all the points that are mapped outside the polygons boundaries. I tried to convert the df as a raster and use the raster::mask function, but when doing this I loose information. I also tried the the rgeos::gDifference, but same problem, can't make the job. Any hint on how I can achieve my goal?
library(rgdal) library(raster) library(sp) library(maptools) library(ggplot2) library(ggmap) # load spatial data fr <- getData('GADM', country = 'France', level = 0) fr <- spTransform(fr, CRS("+proj=longlat +datum=WGS84")) #provide a coordinate system #open df to plot df = read.table("mapdata.txt", header=T, sep="\t", quote="", dec=".") ggplot() + geom_tile(data=df, aes(x=lon, y=lat, fill=item, alpha=prob)) + geom_polygon(data = fr, aes(x=long, y = lat, group = group), fill="transparent", colour = "white", size=1) + theme_nothing(legend = FALSE)