简体   繁体   中英

Difference between id, group and region in fortify{ggplot2}

What exactly is the difference between using id , group and region when one uses fortify{ggplot2} to convert a SpatialPolygonsDataframe to a data.frame ? The documentation is not very clear about the benefit / implication of using these arguments. Any ideas on these as well ?

Here is a reproducible example:

library(ggplot2)
library(UScensus2000tract)

# load data
  data("oregon.tract")

# fortify
  oregon_noth <- fortify(oregon.tract)
  oregon_id <- fortify(oregon.tract, id="tract")
  oregon_grp <- fortify(oregon.tract, group="tract")
  oregon_reg <- fortify(oregon.tract, region="tract")

identical(oregon_noth, oregon_id)
>[1] TRUE

identical(oregon_id, oregon_grp)
>[1] TRUE

identical(oregon_id, oregon_reg)
>[1] FALSE

Using ggplot2:::fortify.SpatialPolygonsDataFrame we can see what is going on:

function (model, data, region = NULL, ...) 
{
    attr <- as.data.frame(model)
    if (is.null(region)) {
        coords <- plyr::ldply(model@polygons, fortify)
        message("Regions defined for each Polygons")
    }
    else {
        cp <- sp::polygons(model)
        unioned <- maptools::unionSpatialPolygons(cp, attr[, 
            region])
        coords <- fortify(unioned)
        coords$order <- 1:nrow(coords)
    }
    coords
}

All the ... arguments are completely discarded. So you can pass id or group without an error, but the output should be identical to only defining model .

I'm not sure why you are using id or group .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM