# What does shading indicate in home range plot?

I've simulated a home range in R using the clusthr function from the adehabitatHR package. The home range is made from 50 x,y coordinates. The output is attached.

I'm trying to get clear on what the shading indicates. The shading is clearly related to the size of the polygon. Smaller polygons = darker shading, larger polygons = paler shading. But how exactly is a polygon assigned a shade? Does the shading represent a percentage of the area covered?

-

This does not seem to be documented in the `clusthr` manual page, but because the source code is available, we can try to figure it out.

By typing

``````> clusthr
``````

at the `R` prompt (shown as the initial `>`), you can see the code. It's opaque, but a quick look indicates (1) there's no plotting or color selection going on and (2) the clustering is performed with a call to a C function. There's no plotting because this code only produces an object that will be plotted later with `plot`.

The version of `plot` to use is determined by the class of the object returned by `clusthr`: the docs say this is a class named "MCHu". Therefore we inspect the code of its corresponding plotting function by typing

``````> plot.MCHu
``````

Here is a key part of the output:

``````...
co <- grey(c(1:nrow(df))/nrow(df))
plot(x, col = co, ...)
...
``````

Evidently the colors are graduated from dark to light in even increments and assigned to polygons in the order they are found in the `MCHu` object. At this point we do some reverse-engineering by generating such an object using the example from the `clusthr` manual page:

``````> data(puechabonsp)
> lo<-puechabonsp\$relocs[,1]
> res <- clusthr(lo)
``````

A request `str(res)` to inspect the structure of `res` is met with a huge amount of output, which is therefore almost worthless, but it's evident this is a list, so let's try to inspect the structure of just its first element:

``````> str(res[[1]])
``````

This time the output is manageable (although still ridiculously long for such a small amount of information). It begins

``````Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data       :'data.frame': 22 obs. of  2 variables:
.. ..\$ percent: num [1:22] 10 13.3 23.3 26.7 36.7 ...
.. ..\$ area   : num [1:22] 0.0179 0.0457 0.0664 0.0947 0.2717 ...
..@ polygons   :List of 22
``````

OK, the polygons are what we're interested in, so let's inspect their structure, but this time we will be a little cautious. Since they are a list, let's inspect just the first one:

``````> res[[1]]@polygons[[1]]

An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1]  698994.7 3161001.7

Slot "area":
[1] 179
...
``````

OK--in the `area` slot there is something interesting related to the hypothesis that shading corresponds to polygon size. (There are actually two `area` slots in this object: probably somebody extended a class that includes an `area` slot but they inadvertently added another; only one of them will be accessible through the `@area` construct.) Let's print out those areas, in order:

``````> sapply(1:length(res[[1]]@polygons), function(i) res[[1]]@polygons[[i]]@area)

[1]    179.0    456.5    663.5    947.0   2717.0   7928.5   9761.5  12790.5
[9]  14839.0  16404.0  18100.0  18325.0  23731.5  37658.5  58793.0  70735.5
[17]  82063.0 105812.5 182443.0 226467.0 252571.5 405963.5
``````

The polygons appear to be sorted by increasing area. This is true for the other three components of `res` (as we may find by repeating the preceding command, replacing `res[[1]]` by `res[[2]]`, etc.) Assuming it is always the case that `clusthr` orders the polygons by increasing area, we may tentatively conclude that lightness corresponds to the ranks of the polygon areas, with lighter polygons having larger area.

-
Such a great breakdown of how to debug in R. Insightful! – SaultDon Feb 10 '13 at 19:08