[R] Creating a point pattern with spatstat

David L Carlson dcarlson at tamu.edu
Mon Apr 16 21:32:31 CEST 2012


First you should read the documentation carefully and look at the examples.
Your data are tabular. Spatstat expects a row for each point with the x and
y coordinates for that point.

# First use dput() to create a version of the data that you can copy into
your r-help requests so we
# can easily generate the data set:

Lv2.8 <- structure(list(X1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L), X2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
  X3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X4 = c(1L, 
  1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), X5 = c(1L, 0L, 1L, 
  1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L), X6 = c(1L, 1L, 1L, 0L, 1L, 
  1L, 1L, 1L, 0L, 1L, 1L, 0L), X7 = c(1L, 1L, 1L, 1L, 1L, 0L, 1L, 
  1L, 1L, 1L, 1L, 0L), X8 = c(1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
  0L, 1L, 0L), X9 = c(1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 
  1L), X10 = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), 
  X11 = c(1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L), 
  X12 = c(0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L)), .Names = c("X1",

  "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "X11", 
  "X12"), class = "data.frame", row.names = c(NA, -12L))

# Now generate matrices of coordinates x=1:12 and y=1:12 and combine them

x <- matrix(col(Lv2.8), nrow=144, ncol=1)
y <- matrix(row(Lv2.8), nrow=144, ncol=1)
xy <- cbind(x, y)

#  Now a matrix of the 1/0 values
z <- matrix(as.matrix(Lv2.8), nrow=144, ncol=1)

# Finally eliminate the coordinates that are empty
points <- xy[as.logical(z),]

# Now your commands will work (with the window expanded slightly)
X <- as.ppp(points, owin(c(0, 13), c(0,13)))
plot(X)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352




> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of AMFTom
> Sent: Monday, April 16, 2012 11:25 AM
> To: r-help at r-project.org
> Subject: [R] Creating a point pattern with spatstat
> 
> I have presence/absence data in the following format:
> 
> 	1	2	3	4	5	6	7	8	9
10	11
> 	12
> 1	1	1	1	1	1	1	1	1	1
1	1
> 	0
> 2	1	1	1	1	0	1	1	1	1
1	1
> 	0
> 3	1	1	1	1	1	1	1	1	1
1	0
> 	1
> 4	1	1	1	1	1	0	1	1	1
1	0
> 	1
> 5	1	1	1	1	1	1	1	0	0
1	1
> 	1
> 6	1	1	1	0	0	1	0	0	1
1	1
> 	1
> 7	1	1	1	1	0	1	1	0	0
0	1
> 	1
> 8	1	1	1	1	1	1	1	0	0
0	1
> 	0
> 9	1	1	1	1	0	0	1	0	0
0	1
> 	1
> 10	1	1	1	1	1	1	1	0	0
0	0
> 	1
> 11	1	1	1	1	1	1	1	1	1
0	0
> 	1
> 12	1	1	1	1	1	0	0	0	1
1	1
> 	1
> 
> Which I want to use to create a ppp object in spatstat. I tried:
> 
> > X <- as.ppp(Lv2.8, owin(c(0, 12), c(0,12)))
> 
> and got
> 
> > plot(X)
>        0        1
> 0.000000 1.187939
> 
> http://r.789695.n4.nabble.com/file/n4562047/attempt1_ppp.jpg
> 
> Which obviously isn't what I'm after. To obtain a simple graphic of
> where
> points are present on the plot, should I convert the raw data to
> non-numerical values?
>  Thanks,
> Tom
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Creating-a-
> point-pattern-with-spatstat-tp4562047p4562047.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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