The basic idea is something like:
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(df, aes(x, y)) + geom_point()
hull <- df[chull(df$x, df$y), ]
ggplot(df, aes(x, y)) +
geom_point() +
geom_polygon(data = hull, fill = NA, colour = "grey50")
If you have multiple groups, you'd need to use ddply or similar to
compute the convex hull for each group.
Hadley
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Something like this:
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100),
z = sample(letters[1:5], 100, rep = T))
ggplot(df, aes(x, y, colour = z)) + geom_point()
find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- ddply(df, "z", find_hull)
ggplot(df, aes(x, y, colour = z)) +
geom_point() +
geom_polygon(data = hulls, fill = NA)
But be careful with the use of hulls. The tend to make clusters look
more distinct than they really are (due to the gestalt of
connectedness/containedness)
df$cl <- factor(kmeans(df[c("x", "y")], 3)$cluster)
hulls <- ddply(df, "cl", find_hull)
# No obvious clusters
ggplot(df, aes(x, y, colour = cl)) +
geom_point()
# Now clusters are really obvious!!
ggplot(df, aes(x, y, colour = cl)) +
geom_point() +
geom_polygon(data = hulls, fill = NA)
Hadley