Life in N-Dimensions

reducing the dimensionality of life

minimum convex polygons

When you’re working with points, be it in geographical space, environmental space, ordination space, or outer space, you often want to generate a shape that neatly connects the outside dots and contains all the points inside. This is called a minimum convex polygon (mcp), or convex hull. It is often used in niche modeling to select background points from, as you want your background points to reflect areas that the species is able to move to, and not areas that may be inaccessible.
Depending on your study, your definition of inaccessibility for the species may be in the present, or could extend far back in time. For example, if a mountain range sprung up over the time period of study and isolated your species to one side, you may consider areas on the other side accessible.
In R, the base function chull() returns the indices of your point coordinate table that make up the borders of the mcp. This is not the most intuitive product to put to use for many people, so I made a nice helper function to return a neat mcp polygon. It handles dataframes, matrices, and spatial objects too.

mcp <- function (xy) {
  # handler for spatial objects -- extracts coordinates
  if (class(xy) == "SpatialPoints" | class(xy) == "SpatialPointsDataFrame") {
    xy <- as.data.frame(coordinates(xy))
  }
  # get mcp indices
  i <- chull(xy)
  # get rows in xy for i
  xy.mcp <- xy[i,]
  # copy first row to last position to close shape
  xy.mcp <- rbind(xy.mcp[nrow(xy.mcp),], xy.mcp)
  # return polygon of mcp
  return(SpatialPolygons(list(Polygons(list(Polygon(as.matrix(xy.mcp))), 1))))
}

zoom to point

I do lots of spatial stuff in R. I used to be an ArcGIS guy in the days when I was a GIS Analyst, but I made the switch when I got (back) to grad school. At first, the only reason I shrugged off ESRI products was because I thought I’d be cooler if I used only open source software. I did become cooler, much cooler. And I retain my love/hate relationship with ArcGIS, mostly because I am accustomed to it (I use ArcGIS operating on muscle memory). But there is arguably one thing that GIS software is superb for: panning and zooming quickly around big maps. I find myself using R for nearly all the spatial stuff I do now, but there are still times when I grudgingly have to trudge back to ArcGIS for ease of navigation. Zooming and navigating around on plots in R is not exactly smooth.

That’s why I wrote a little function to zoom to points on a raster. It’s not multi-purpose by any means, but you can hack it to do anything you want. This makes examining raster values around points a cinch.

# r = raster to zoom into
# p = single point coordinates you want to zoom to, 
# as vector c(lat, lon)
# rad = radius of zoom in units of projection

zoom2pt <- function(r, pt, rad) {
  ext <- c(pt[1]-rad, pt[1]+rad, pt[2]-rad, pt[2]+rad)
  plot(r, ext=ext)
  points(pt[1], pt[2])
}

And here’s another simple function that zooms around to multiple points. Just press Return to go to the next one. This only works on dataframes of point locations, and will error if you input just one set of coordinates. Keep in mind that if you escape the function before it finishes, par(ask=TRUE) will remain on — just set it to FALSE.

# r = raster to zoom into
# p = multiple point coordinates you want to zoom to, 
# as dataframe with columns (lon, lat)
# rad = radius of zoom in units of projection

zoomAround <- function(r, pts, rad) {
  np <- nrow(pts)
  par(ask=TRUE)
  for (i in 1:np) {
    zoom2pt(r, pts[i,], rad)
  }
  par(ask=FALSE)
}

Hello world… of science blogging

Greetings. I am a PhD student in Biology at City College / CUNY Graduate Center in NYC. My focus is biogeography, and I study techniques in species distribution modeling and how interactions between species affect these distributions. I am also a recovering writer who suffers from frequent, crushing lapses of inspiration. Hence, this noble effort.

I will be posting interesting code, poignant criticism of the current state of ecology, and iniquitous comments about science.

Skip to toolbar