Girija Kalyani
2014-Sep-27 08:36 UTC
[R] could not plot my spatial species points on the raster data
Dear Group, I working with species distribution modeling in R. I have a raster file (stacked with environmental layers around 24 layers), i have a shape file. Both the arster and shape file are of same projections. I confirmed it. I have a csv file read which has my species data, but the problem here is my species aren't getting plotted on the raster data. What could be the proble. raster <- raster(choose.files()) plot(raster) #input the species occurence file hippo <-read.csv(choose.files()) hippo <- read.table(hippo, header=TRUE, sep=",") hippo <- hippo[,2:3] points(hippo$lon,hippo$lat,col="blue",pch=19,cex=0.75, xlab="Longitude",ylab="Latitude") map<- readOGR(".", layer= 'lahul') plot(map, col="grey") hip <- data.frame(hippo$lon,hippo$lat) points(hip$lon,hip$lat,col="blue",pch=19,xlab="Longitude",ylab="Latitude") ...... Any help wil be highly appreciated. Thanx in advance [[alternative HTML version deleted]]
MacQueen, Don
2014-Sep-29 21:56 UTC
[R] could not plot my spatial species points on the raster data
Possibly, not much help is possible without the data, or a reproducible example. This is probably better asked on r-sig-geo A few more comments, inserted below -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/27/14, 1:36 AM, "Girija Kalyani" <smileismystyl at gmail.com> wrote:>Dear Group, > >I working with species distribution modeling in R. >I have a raster file (stacked with environmental layers around 24 layers), >i have a shape file. >Both the arster and shape file are of same projections. >I confirmed it. >I have a csv file read which has my species data, but the problem here is >my species aren't getting plotted on the raster data. What could be the >proble. > > >raster <- raster(choose.files()) >plot(raster)raster() is a function, it is a bad idea to also give one of your own objects the same name>#input the species occurence file >hippo <-read.csv(choose.files())This gives you a data frame, but given what you do next, you need a character string instead. In my R it is file.choose() Therefore, you should do hippo <- file.choose()>hippo <- read.table(hippo, header=TRUE, sep=",") >hippo <- hippo[,2:3]hippo <- hippo[,2:3] is not necessary, but also harmless. But make sure that columns 2 and 3 are ?lon? and ?lat? ( or ?lat? and ?lon?), otherwise it?s a mistake. Better would be hippo <- hippo[ , c(?lon?,?lat?)]>points(hippo$lon,hippo$lat,col="blue",pch=19,cex=0.75, >xlab="Longitude",ylab="Latitude")xlab and ylab are normally arguments to a plot() command, not a points() command try require(sp) bbox(raster) followed by range(hippo$lon) range(hippo$lat) are the ranges of lon and lat within the bounding box? Now you are starting a second plot. Which one is it that you want?>map<- readOGR(".", layer= 'lahul') >plot(map, col="grey") >hip <- data.frame(hippo$lon,hippo$lat)The above may be the problem. Probably, the columns in hip are not named ?lon? and ?lat?. What does names(hip) return? Better would be hip <- hippo[ , c(?lon?,?lat?) ] But in fact this is pointless, because now hip and hippo are identical.>points(hip$lon,hip$lat,col="blue",pch=19,xlab="Longitude",ylab="Latitude")Try replacing the whole thing with ## get the raster inrast <- file.choose() myraster <- raster(inrast) #input the species occurence file infile <- file.choose() hippo <- read.table(infile, header=TRUE, sep=?,?) require(sp) coordinates(hippo) <- c(?lon?,?lat?) proj4string(hippo) <- CRS(proj4string(myraster))) plot(myraster) plot(hippo, col="blue?, pch=19, add=TRUE) ## you could also try plot(map, col=?grey?) plot(hippo, col="blue?, pch=19, add=TRUE)> >...... > Any help wil be highly appreciated. >Thanx in advance > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.