[R] could not plot my spatial species points on the raster data

MacQueen, Don macqueen1 at llnl.gov
Mon Sep 29 23:56:47 CEST 2014


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.



More information about the R-help mailing list