Search code examples
rgithubgislatitude-longituderaster

How to successfully fit a map into a longitude/latitude bounding box with the correct geographical scale and plot GPS points


Outline of the Issue

I am sorry for asking a simple question, but I am new to R and I am experiencing difficulties performing tasks with maps.

I have a collection of longitude and latitude GPS points in decimal form, which were collected in the field. My aim is to plot these GPS points onto a map of Sri Lanka, which I extracted from GADM resources.

After running the code, the southern tip of Sri Lanka is protruding from the top middle of the longitude/latitude grid box rather than the whole image of Sri Lanka being visible within the longitude/latitude grid box (see image 2).

Problem:

I can produce the map of Sri Lanka independently (see image 2), and the longitude/latitude grid box separately (see image 1). However, I am having trouble plotting the map of Sri Lanka inside the latitude/longitude grid box, in conjunction with plotting the GPS points within the grid box in the correct positions that the data was collected in the field.

The desired output is evidenced in image 3 (see below). I am trying to place image 1 inside the grid box with the correct longitude/latitude scale for Sri Lanka on the edge of the grid box. Finally, I would like to plot the GPS points on the map, just like the provided example in image 3.

If anyone would be able to help me, I would be incredibly grateful!

I really cannot figure out what is going wrong here due to my lack of knowledge and after many hours of trying different R code combinations in order to solve the issue by attempting to reproduce this stack overflow question and by following this exercise on species distribution modeling.

Very kind regards.

R-code

##Libraries that are going to be used:

   library("sp")
   library("raster")
   library("maptools")
   library("rgdal")
   library("dismo")
   library("spatialEco")
   library("ggplot2")
   library("dplyr")

###Open the directory pathway

 Blue.whale<-readr::read_csv("Blue_Whale_GPS_Best.csv")
 summary(Blue.whale)

##Plotting the map of Sri Lanka
   bioclim1.data <- getData('GADM', country='LKA', level=1)
   Sri_Lanka<-plot(bioclim1.data, main="Adm. Boundaries Sri Lanka Level 0")


 ###My attempt at creating a longitude/latitude grid box

    Sri.Lanka.bbox<-bbox(Blue.whale)
    xlim <- c(min(Sri.Lanka.bbox[1,1]), max(Sri.Lanka.bbox[1,2]))
    ylim <- c(min(Sri.Lanka.bbox[2,1]), max(Sri.Lanka.bbox[2,2]))

  ###Plot the longitude/latitude grid box
     dev.new()
     plot(Sri_Lanka, xlim=xlim, ylim=ylim, add=T)


 ##Plot map
   par(mfrow=c(1,1))
   dev.new()

####Convert the format of the data from factors to numeric

    Latitude<-as.numeric(Blue.whale$Latitude)
    Longitude<-as.numeric(Blue.whale$Longitude)

##To make species distribution modeling more streamlined, it is useful to have an 
##idea of how widely our species is geographically distributed. We are going to find 
 ##general latitudinal and longitudinal boundaries and store this information:

  # Determine geographic extent of our data
    max.lat <- ceiling(max(Blue.whale$Latitude))
    min.lat <- floor(min(Blue.whale$Latitude))
    max.lon <- ceiling(max(Blue.whale$Longitude))
    min.lon <- floor(min(Blue.whale$Longitude))
    geographic.extent <- extent(x = c(min.lon, max.lon, min.lat, max.lat))

     # Plot the base map

       dev.new()

       plot(bioclim1.data, 
            xlim = c(min.lon, max.lon),
            ylim = c(min.lat, max.lat),
            axes = TRUE, 
            col = "grey95")

       # Add the points for individual observation
         points(x = Blue.whale$Longitude, 
                y = Blue.whale$Latitude, 
                col = "olivedrab", 
                pch = 15, 
                cex = 0.50)

Image 1:

enter image description here

Image 2:

enter image description here

Image 3:

enter image description here


Solution

  • For Example 3 they have cropped the map of USA to focus on where that species occurred, whereas you want to show where the whale sightings occurred in relation to the whole country of Sri Lanka. To show both the whole country and all of the sightings you need to change your plot limits to match the extremities of the two data sources. This code should produce your desired plot, you can add ceiling / floor arguments to improvement aesthetics if needed:

    ##get bounding box of Sri Lanka shapefile
    bb=bioclim1.data@bbox
    
    plot(bioclim1.data, 
         xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
         ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
         axes = TRUE, 
         col = "grey95")
    
    # Add the points for individual observation
    points(x = Blue.whale$Longitude, 
           y = Blue.whale$Latitude, 
           col = "olivedrab", 
           pch = 15, 
           cex = 0.50)