[GIS] Remove points outside a map with ggplot2

ggplot2rraster

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) 

Best Answer

Personally I'd ditch ggplot and use raster as much as possible.

But here's a solution.

Work out which points are in and which are out of France:

inout = over(
  SpatialPoints(df[,c("lon","lat")],proj4string=CRS(projection(fr))),
  as(fr,"SpatialPolygons")
)

Then ggplot the subset that isn't NA:

> ggplot() +
   geom_tile(data=df[!is.na(inout),],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) 

enter image description here