1

The set up is this: There are 10 trees within a 20 by 20 m quadrat in a forest. For each tree we know the species, the diameter (in cm), and the location within the quadrat using x,y coordinates.

I would like to plot the trees within the quadrat, where the size of the points are to scale, and each species is represented by a different colour circle.

Use this data for an example:

tag <- as.character(c(1,2,3,4,5,6,7,8,9,10))

species <- c("A","A","A","A","B","B","B","C","C","D")

diameter <- c(50,20,55,30,30,45,15,20,35,45)

x <- c(9,4,5,14,8,19,9,12,10,2)

y <- c(6,7,15,16,12,4,19,2,14,9)

df <- data.frame(tag, species, diameter, x, y)

First I create the point pattern

species_map <- ppp(df$x, df$y, c(0,20), c(0,20))

Then I mark the species and diameter

marks(species_map) <- data.frame(m1 = df$species, m2=(df$diameter))

Now I can plot the point pattern and each point is to scale thanks to the marks on the diameter. The "markscale" bit is set to 0.01 because the diamter measurements are in cm and the quadrat size is defined in meters.

plot(species_map, which.marks=2, markscale=.01)

Now I want to make the circles of different species different colours, but this is where I'm stuck.

If I try to make a plot that includes both of my marks I just get 2 separate plots, with one using different size points to represent diameter (correctly) and one using different characters to represent different species.

plot(species_map, which.marks= c(1,2), markscale=.01)

How can I get this plot to represent different species using different colors of the same character while ALSO plotting the points to scale?

And how can I make it produce 1 single plot?

Thank you in advance.

Jay

markalex
  • 8,623
  • 2
  • 7
  • 32
Jay
  • 157
  • 1
  • 2
  • 9

3 Answers3

2


Strangely enough I can't think of a really elegant way to do this. My best bet is to split the data into separate point patterns by species and loop through the species and plot. Is that enough for you?

library(spatstat)
tag <- as.character(c(1,2,3,4,5,6,7,8,9,10))
species <- c("A","A","A","A","B","B","B","C","C","D")
diameter <- c(50,20,55,30,30,45,15,20,35,45)
x <- c(9,4,5,14,8,19,9,12,10,2)
y <- c(6,7,15,16,12,4,19,2,14,9)
df <- data.frame(tag, species, diameter, x, y)
species_map <- ppp(df$x, df$y, c(0,20), c(0,20))
marks(species_map) <- data.frame(m1 = df$species, m2=(df$diameter))

You need to choose four colours and fix the same range of diameters in each plot and the do the loop (argumet bg is passed to symbols and fills the background of the circles with this colour):

diamrange <- range(diameter)
cols <- c("black", "red", "green", "blue")
species_map_split <- split(species_map, reduce = TRUE)
plot(species_map_split[[1]], markrange = diamrange, markscale=.01,
     main = "", cols = cols[1], bg = cols[1])
#> Warning: Interpretation of arguments maxsize and markscale has changed (in
#> spatstat version 1.37-0 and later). Size of a circle is now measured by its
#> diameter.
for(i in 2:4){
  plot(species_map_split[[i]], markrange = diamrange, markscale=.01,
       add = TRUE, col = cols[i], bg = cols[i])
}

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
1

Symbol maps for multiple columns of marks are not yet implemented in spatstat. So you'll need to do something like Ege suggests.

Adrian Baddeley
  • 1,956
  • 1
  • 8
  • 7
1
species  <- c("A","A","A","A","B","B","B","C","C","D")
diameter <- c(50,20,55,30,30,45,15,20,35,45)
x        <- c(9,4,5,14,8,19,9,12,10,2)
y        <- c(6,7,15,16,12,4,19,2,14,9)
library(spatstat)
Dat <- data.frame(x,y,species, diameter)
X   <- as.ppp(Dat,W=square(20))
marks(X)$species <- factor(marks(X)$species)
ccc <- c("red","green","blue","black")[as.numeric(marks(X)$species)]
plot(X,which.marks="diameter",maxsize=1,main="Elegant?")
plot(X,which.marks="diameter",maxsize=1,bg=ccc,add=TRUE)

Elegant Or What?!!!

#thanks to @RolfTurner for this!

  • I've been talking to Rolf Turner about this and have reproduced his code here as an answer to a new question on using multiple species and also inserting a legend : https://stackoverflow.com/questions/69864435/how-do-i-create-a-combined-colour-plot-with-legend-overlaying-two-marks-in-spats/69864436#69864436 Happy plotting! – David Cracknell Nov 06 '21 at 13:50
  • Sorry, but this will not work in future versions of `spatstat`. This code exploits a bug in the current `spatstat` code which will be closed soon. You'll need to use base graphics to do this plot soon. – Adrian Baddeley Nov 07 '21 at 03:28