[R-sig-Geo] Plotting Kernel results with ggplot2

Andrew Allyn andrew.allyn at gmail.com
Tue Mar 1 13:55:09 CET 2016


Hello Diego,

Hopefully I am submitting this correctly -- first time answering a 
question rather than asking one on here. I think the following code 
should do what you want.

- Andrew

##### Start -- after running your preliminary code and getting the 50 
and 75 kernel contour vertices from the kernelUD object
### Using ggplot2 geom_holygon
library(ggplot2)
library(proto)
library(grid)

## Define geom_holygon function, from 
http://rstudio-pubs-static.s3.amazonaws.com/3522_52420d28ca7d443eae79850822ead5b8.html
GeomHolygon <- ggproto(
   "GeomHolygon",
   GeomPolygon,
   extra_params = c("na.rm", "rule"),
   draw_panel = function(data, scales, coordinates, rule) {
     n <- nrow(data)
     if (n == 1)
       return(zeroGrob())

     munched <- coord_munch(coordinates, data, scales)
     munched <- munched[order(munched$group), ]

     first_idx <- !duplicated(munched$group)
     first_rows <- munched[first_idx, ]

     ggplot2:::ggname(
       "geom_holygon",
       pathGrob(munched$x, munched$y, default.units = "native",
                id = munched$group, rule = rule,
                gp = gpar(col = first_rows$colour,
                          fill = alpha(first_rows$fill, first_rows$alpha),
                          lwd = first_rows$size * .pt,
                          lty = first_rows$linetype)))
   }
)

geom_holygon <- function (mapping = NULL, data = NULL, stat = 
"identity", position = "identity",
                           na.rm = FALSE, show.legend = NA, inherit.aes 
= TRUE, rule = "winding", ...) {
   ggplot2::layer(data = data, mapping = mapping, stat = stat, geom = 
GeomHolygon,
                  position = position, show.legend = show.legend, 
inherit.aes = inherit.aes,
                  params = list(na.rm = na.rm , rule = rule, ...))
}


## Plotting
# "Fortify" polygons
kern.50.df<- fortify(ver.sim50)
kern.75.df<- fortify(ver.sim75)

# Plot
ggplot() +
   geom_holygon(data = kern.75.df, aes(x = long, y = lat, group = 
group), fill = "steelblue") +
   geom_holygon(data = kern.50.df, aes(x = long, y = lat, group = 
group), fill = "#8CC739") +
   theme_bw()

# Note: for this simple example, it doesn't look like there are any 
holes in your kernel contours SpatialPolygonsDataframe object. The real 
advantage of the geom_holygon that I noticed was when you do have holes, 
or different regions with 50% or 75% contours. This function is able to 
plot just the filled areas more effectively than I was able to do either 
by subsetting the SpatialPolygons to remove holes or by forcing holes to 
be white/not filled.

## A different option, using geom_path
ggplot() +
   geom_path(data = kern.75.df, aes(x = long, y = lat), colour = 
"steelblue") +
   geom_path(data = kern.50.df, aes(x = long, y = lat), colour = 
"#8CC739") +
   theme_bw()

# Note, not sure how the geom_path would behave with holes in the 
polygons as discussed above.

##### End code



More information about the R-sig-Geo mailing list