0
votes

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

1
Could you please provide a minimal dataset as a data frame to make your question reproducible minimal reproducible example. Something along the lines of Blue.whale <- data.frame(....) where ... represents a sample of values for each of the variables in the dataset which result in the error noted in your question. The section "Insert GPS Points" seems to include an undefined object: Blue.whale_New. But I think the real problem is that your map is of class ggplot and the documentation for GADMTools::dots requires an object gadm_sp or gadm_sfPeter
Hi Peter. Thank you for answering my question. I ran your code separately and it worked very well thank you but it did not work on my data frame. I will add a reproducible version tomorrow but I cannot put my data online. My map is in gadm_sf format as I created it using gadm_sf_loadcountries() and I also changed my data to a data frame format and I am still getting the same error message. I am not sure if the data frame is the problem. I do not mind sending you my data frame privately to try out the code you wrote as the answer. I can’t figure out what is wrong. Many thanks for your helpAlice Hobbs
I also changed my data frame so longitude is the first column then latitude. I still feel confused!Alice Hobbs
Does your data frame include only the two variables: longitude and latitude?Peter
Yes! Initially, there were three columns as seen in the example above but I created a new data frame with just longitude and latitude using data.frame()Alice Hobbs

1 Answers

1
votes

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)