Here is some code and a few recommendations for creating spatially-explicit plots using R and the ggplot and sf packages.
Lets suppose that we want to plot country outlines and occurrence points for two species of animals. Without having to download Shapefiles or import spreadsheets, we can use data bundled with or imported by rnaturalearth (a package from the rOpenSci crew that interacts with Natural Earth, a public domain spatial dataset). We’ll be generating random points to represent point occurrence data for the two species of animals.
We’ll work with Senegal for this post. We can assign an sf (simple features) object for all of Africa, and then filter it by country name. After that, random points for our two hypothetical species can be generated within the Senegal polygon using st_sample.
We can already plot these two objects with ggplot. The map looks OK, and we haven’t even tweaked any of the plot arguments.
If we change the point size for visibility and make them larger, some will surely overlap and we might overlook them. To solve this clutter, I took Simon Jackson’s advice from this post of setting the point transparency in relation with local point density (inversely proportional). If there are lots of overlapping points, they are more transparent, while spatially isolated points remain opaque.
To accomplish this, we need the coordinates from the sf object (they are held in a list-column). Then we estimate the spatial point density using the pointdensityP package. I went with a pretty narrow grid size and search radius here. We then join the densities with our sf point object and scale the values between 0 and 1.
be aware that the pointdensity function arranges the output table by density, so we need to merge or join this with our sf object. If we simply bind the columns we lose all sense of the actual densities for each point (I learned that the hard way).
We can also improve our map by adding some geographic context. Plotting the neighboring countries can add this useful context. The st_touches functions tells us which features share boundaries with our target object, Senegal. This way, we can assign a new object with the neighbors by slicing the one we had for all of Africa.
Lastly, we can customize the shapes, colors, borders, fills, and transparency values. I often use the st_bbox and st_buffer functions to set the plot limits (fed into coord_sf) and focus the plot on our target feature. A few other arguments can help us hide the gridlines and add informative titles to the plot and to the legend. The final result looks pretty crisp.
To compare the two figures, this sequence of piped functions show how well purrr, fs, and magick work to read the image files for our plots and create a .gif animation with the two.
Pretty cool! Feel free to contact me with feedback or questions :)