[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

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

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
>raster <- raster(choose.files())

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

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¹)]


xlab and ylab are normally arguments to a plot() command, not a points()

followed by
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

Better would be
  hip <- hippo[ , c(Œlon¹,¹lat¹) ]
But in fact this is pointless, because now hip and hippo are identical.


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=³,²)

coordinates(hippo) <- c(Œlon¹,¹lat¹)
proj4string(hippo) <- CRS(proj4string(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
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list