rggplot2plotspatstatr-ppp

How do I plot species as different colours in a point pattern (ppp) using spatstat in R?


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


Solution


  • 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])
    }