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

Diego Pavon diego.pavonjordan at gmail.com
Wed Mar 2 10:32:20 CET 2016


Hi Andrew and all

Thanks a lot for this! This was awesome! I could even plot those kernels in
google.maps and other .shp files!
 This was a brilliant piece of code!

Thanks again

Cheers

Diego

2016-03-01 14:55 GMT+02:00 Andrew Allyn <andrew.allyn at gmail.com>:

> 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
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
*Diego Pavón Jordán*

*Finnish Museum of Natural History*
*PO BOX 17 *

*Helsinki. Finland*



*0445061210https://tuhat.halvi.helsinki.fi/portal/en/persons/diego-pavon-jordan%288d5db37c-eddd-4fca-92cd-9c9956a42b4a%29.html
<https://tuhat.halvi.helsinki.fi/portal/en/persons/diego-pavon-jordan%288d5db37c-eddd-4fca-92cd-9c9956a42b4a%29.html>http://www.linkedin.com/profile/view?id=170617924&trk=nav_responsive_tab_profile
<http://www.linkedin.com/profile/view?id=170617924&trk=nav_responsive_tab_profile>https://helsinki.academia.edu/DiegoPavon
<https://helsinki.academia.edu/DiegoPavon>*

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list