Search code examples
rgpsmapslatitude-longituderaster

Using the dots() function within the GADMTools package in R for maps


Problem Outline:

I have created a map of Sri Lanka using the functions gadm_sf_loadCountries() and plotmap() functions in the GADMTools package (see code below). Therefore, my map (see image below) is in gadm_sf format.

My aim is to plot my GPS points contained a .csv file called 'Blue.whale' onto the map I produced using the dots() function in the GADMTools package.

However, when I run my code, I am receiving this error message:

Error in UseMethod("dots", x) : 
 no applicable method for 'dots' applied to an object of 
 class "c('gg', 'ggplot')"

R-code:

##Load Libraries

  library(GADMTools)
  library(sp)

####GADMTools Version
    dev.new()
    bioclim2.data <- gadm_sf_loadCountries("LKA", basefile = "./", level = 1) 

##Plot map
  par(mfrow=c(1,1))
  Sri_lanka<-plotmap(bioclim2.data)

###

Blue.whale<-readr::read_csv("Blue_Whale_GPS_Best.csv")
colnames(Blue.whale)<-c("FID", "Latitude", "Longitude")
head(Blue.whale)

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

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

 ##Insert GPS Points
   Blue.whale$Latitude<-as.double(Blue.whale$Latitude)
   Blue.whale$Longitude<as.double(Blue.whale$Longitude)
   dots(Sri_lanka, points=Blue.whale, color="red", size=8)

Here is a sample of the data frame with longitude and latitude coordinates. In total, there are another 908 rows

     # A tibble: 918 x 3
       FID Latitude Longitude
      <dbl>    <dbl>     <dbl>
   1     1     5.80      80.6
   2     2     5.84      80.5
   3     3     5.82      80.5
   4     4     5.85      80.5
   5     5     5.85      80.5
   6     6     5.89      80.4
   7     7     5.82      80.4
   8     8     5.82      80.5
   9     9     5.84      80.5
  10    10     5.83      80.4

If anyone can help with this error message, then I would be deeply appreciative.

Best wishes!

Map

enter image description here


Solution

  • This may help...

    There's alot of code in your question that I do not really understand (possibly built-up from various sources).

    Some issues with your code were:

    1) the points argument in the dots function requires a dataframe of x and y coordinates in lat and long terms this is longitude and latitude in that order and all in lower case.

    2) the x object for the dots function requires an object gadm_sp or gadm_sf

    3) There seemed to be an undefined object: Blue.whale_New.

    Anyhow, this seems to do the trick:

    
    
    library(GADMTools)
    
    Blue.whale <- data.frame(longitude = c(80.5, 80.5, 80.5, 80.5, 80.4, 80.4, 80.5, 80.5, 80.4),
                             latitude = c(5.84, 5.82, 5.85, 5.85, 5.89, 5.82, 5.82, 5.84, 5.83))
    
    
    Sri_lanka <- gadm_sf_loadCountries("LKA", basefile = "./", level = 1)
    
    dots(x = Sri_lanka, points = Blue.whale, color = "red", size = 3)
    
    

    Created on 2020-05-11 by the reprex package (v0.3.0)