0

I'm attempting to create a raster layer of lizard location data. Everything seems to be working until my final line of code creating the raster:

# Create raster
utm.points <- cbind(data.TM$X, data.TM$Y) # create an object of UTMs dataframe

utm.locations <- SpatialPoints(utm.points, proj4string=CRS("+proj=utm +zone=12 +datum=NAD27")) # create a spatial projection

proj.lat.lon <- as.data.frame(spTransform(utm.locations, CRS("+proj=longlat +datum=NAD27"))) # transform UTMs to lat/lon

colnames(proj.lat.lon) <- c("x","y") # rename columns of lat/lon object

raster <- openmap(c(max(proj.lat.lon$y)+0.005, min(proj.lat.lon$x)-0.005), c(min(proj.lat.lon$y)-0.005, max(proj.lat.lon$x)+0.005), type = "bing")

raster.utm <- openproj(raster, projection = "+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs") # create the raster

When I run the final line to create the raster.utm I get the following error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘xmax’ for signature ‘"data.frame"’

Here's a sample of my data if that helps

    library(tidyverse)
    library(janitor)
    library(lubridate)
    library(sp)
    library(OpenStreetMap)
    library(pbapply)
    library(mgsub)
    library(stringr)
    library(data.table)

    animal.ID<-c("TM 1","TM 1","TM 1","TM 1")
    X<-c(489755 ,489792 ,489537 ,489505)
    Y<-c(3722898 ,3722405 ,3722746 ,3722467)
  
  
  
    data.TMex<-data.frame(animal.ID,X,Y)


    utm.points <- cbind(data.TMex$X, data.TMex$Y) # create an object of UTMs dataframe
    utm.locations <- SpatialPoints(utm.points, proj4string=CRS("+proj=utm +zone=12 +datum=NAD27")) # create a spatial projection
    proj.lat.lon <- as.data.frame(spTransform(utm.locations, CRS("+proj=longlat +datum=NAD27"))) # transform UTMs to lat/lon
    colnames(proj.lat.lon) <- c("x","y") # rename columns of lat/lon object
    raster <- openmap(c(max(proj.lat.lon$y)+0.005, min(proj.lat.lon$x)-0.005), c(min(proj.lat.lon$y)-0.005, max(proj.lat.lon$x)+0.005), type = "bing")

    raster.utm <- openproj(raster, projection = "+proj=utm +zone=12 +datum=NAD27 +units=m +no_defs")


    autoplot.OpenStreetMap(raster.utm, expand = TRUE) + theme_bw() + theme(legend.position="bottom") +
  theme(panel.border = suppressWarnings(element_rect(colour = "black", fill=NA, size=1))) +
  geom_point(data=data.TMex, aes(X, Y, color= animal.ID), size = 3, alpha = 0.8) +
  theme(axis.title = element_text(face="bold")) + labs(x="Easting", y="Northing") + guides(color=guide_legend("Identifier")) 
Brian
  • 7,900
  • 1
  • 27
  • 41
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's hard to help if we don't know exactly what's in these variables. – MrFlick Feb 15 '23 at 21:13
  • Sorry for not including a sample of my data earlier. I've added a small sample to my post though, and I have absolutely no idea why, this code actually works when I run it in a new r project. I'm using the same exact code with the same exact libraries loaded, the only difference is the new empty r project. I'm not sure if anyone will be able to help since my error is apparently not reproducible with this sample code, but I appreciate any advice! – Jason Edelkind Feb 16 '23 at 18:33
  • I think I figured it out, apparently the ctmm package I had downloaded was somehow interfering with my code. After restarting R and not loading the ctmm package until after running my code, it appears to be working. – Jason Edelkind Feb 22 '23 at 19:34

0 Answers0