Extract contour lines from geom_density2d

132 views
Skip to first unread message

Serge-Étienne Parent

unread,
Sep 20, 2013, 4:00:22 PM9/20/13
to ggp...@googlegroups.com
Hello,

Is it possible to extract the lines, or polygons, or the points used to construct them, from a geom_density2d object?

Example:
library(ggplot2)
library(MASS)
data(geyser, "MASS")
m <- ggplot(geyser, aes(x = duration, y = waiting)) +
  geom_density2d()


In the example case, I would extract from the m object the points used to draw the isocontour lines, transform these points to another scale, then replot them using geom_polygon in the new scale. Drawing contours directly from data transformed in the new scale wouldn't be relevant.

Thanks!

Serge-Éti. Parent
Université Laval
Québec (Canada)

Ista Zahn

unread,
Sep 20, 2013, 5:00:55 PM9/20/13
to Serge-Étienne Parent, ggplot2
m.dat <- ggplot_build(m)[["data"]][[1]]

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages