I am fairly new in doing GIS work in R, but I thought this would be a nice situation to get my feet wet.

Here is the situation: I need to create multiple plots with the same background, the same polygon layer from a shapefile, but a different selection of points (another shapefile), each time an other animal species. So I would think to create a loop where I run through a list of species, create the plot (either ggplot of tmap, or something else) and save the plot as a PNG.

I loaded the shapefiles with st_read() (from the sf package), and making the selection and put them in the plot is fairly straight forward. But I have a problem when I want to add the background map.

A few things I have tried: Download OSM tiles using the read_osm() function from tmaptools. But my shapes are in a local Projection (Amersfoort/RD New) and the map is slightly misaligned with my shapefile for some reason.

Download tiles with get_map() from the ggmap package, but this fails with a '400 Bad Request'

Export a TIFF of the background map I want in QGIS is and try to import it into R using the raster function. This solves the alignment, but the layer is not in colour.

I think the last route might be the simplest, but than my question is: how do I export a map in QGIS that I can import in R to display as a coloured background map in ggplot, ggmap or tmap.

But also other suggestions, I am very new to this, and there is a lot of different information out there so I might be doing this all wrong...

  • It would be helpful if you could edit your post and provide some of your code to help solve the problems you're having. The alignment issue between the OSM tiles and your shapefile is likely because they are in different coordinate systems. R does not reproject on the fly like many desktop GIS platforms so you will need to transform the coordinate system of one of the layers to match the other. As far as the TIFF, how are you trying to display it in R that is not working? Take a look at the plotRGB function in the raster package. – Jacob F Sep 26 '18 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.