1

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) 
2
  • What do you mean you lose information when using raster::mask?
    – Spacedman
    Jul 28, 2017 at 15:47
  • My R wasn't too happy with your data file. Linux thinks its ISO-8859 encoded and it would be much happier if it was UTF-8.
    – Spacedman
    Jul 28, 2017 at 16:03

1 Answer 1

3

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.