hadley/ggplot2 — pull request #771 by wch (2013-02-28 17:34:40+00:00)
Title: Fix polygon grouping bug

Fixes #741 and fixes #749.

The problem is that the `group` returned from `coord_munch` is sometimes a character vector, and it was passed to `polygonGrob` as the `id` value. But `id` must be a numeric vector, according to the help page:

> id: A numeric vector used to separate locations in ‘x’ and ‘y’ into multiple polygons. All locations with the same ‘id’ belong to the same polygon.

Converting it to a factor ensures that it's numeric. I had to use `mode()` because this is one of those times where `is.numeric` is reporting something other than what's desired. What's needed is something that will report TRUE for factors, integers, and floats, and FALSE for strings. `mode()` is the function that does the trick

```R
char <- "A"
fac <- factor("A")
int <- 1L
float <- 1.1

typeof(char)
# [1] "character"
storage.mode(char)
# [1] "character"
mode(char)
# [1] "character"
is.numeric(char)
# [1] FALSE
is.integer(char)
# [1] FALSE

typeof(fac)
# [1] "integer"
storage.mode(fac)
# [1] "integer"
mode(fac)
# [1] "numeric"
is.numeric(fac)
# [1] FALSE
is.integer(fac)
# [1] FALSE

typeof(int)
# [1] "integer"
storage.mode(int)
# [1] "integer"
mode(int)
# [1] "numeric"
is.numeric(int)
# [1] TRUE
is.integer(int)
# [1] TRUE

typeof(float)
# [1] "double"
storage.mode(float)
# [1] "double"
mode(float)
# [1] "numeric"
is.numeric(float)
# [1] TRUE
is.integer(float)
# [1] FALSE
```