[R] Adding 95% contours around scatterplot points with ggplot2
istazahn at gmail.com
Mon Jan 28 19:50:00 CET 2013
This only fits some of your criteria, but have you looked at ?stat_density2d?
On Mon, Jan 28, 2013 at 12:53 PM, Nathan Miller <natemiller77 at gmail.com> wrote:
> Hi all,
> I have been looking for means of add a contour around some points in a
> scatterplot as a means of representing the center of density for of the
> data. I'm imagining something like a 95% confidence estimate drawn around
> the data.
> So far I have found some code for drawing polygons around the data. These
> look nice, but in some cases the polygons are strongly influenced by
> outlying points. Does anyone have a thought on how to draw a contour which
> is more along the lines of a 95% confidence space?
> I have provided a working example below to illustrate the drawing of the
> polygons. As I said I would rather have three "ovals"/95% contours drawn
> around the points by "level" to capture the different density distributions
> without the visualization being heavily influenced by outliers.
> I have looked into the code provided here from Hadley
> using the mvtnorm package and the dmvnorm function, but haven't been able
> to get it work for my data example. The calculated densities are always
> zero (at this step of Hadley's code: dgrid$dens <-
> dmvnorm(as.matrix(dgrid), ex_mu, ex_sigma) )
> I appreciate any assistance.
> data<-data.frame(level=c(rep(1, 30),rep(2,30), rep(3,30)), x=x,y=y)
> find_hull <- function(data) data[chull(data$x, data$y), ]
> hulls <- ddply(data, .(level), find_hull)
> fig1 <- ggplot(data=data, aes(x, y, colour=(factor(level)),
> fig1 <- fig1 + geom_polygon(data=hulls, alpha=.2)
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help