How can a data ellipse be superimposed on a ggplot2 scatterplot?
- by Radu
Hi, I have an R function which produces 95% confidence ellipses for scatterplots. The output looks like this, having a default of 50 points for each ellipse (50 rows):
[,1] [,2]
[1,] 0.097733810 0.044957994
[2,] 0.084433494 0.050337990
[3,] 0.069746783 0.054891438
I would like to superimpose a number of such ellipses for each level of a factor called 'site' on a ggplot2 scatterplot, produced from this command:
> plat1 <- ggplot(mapping=aes(shape=site, size=geom), shape=factor(site)); plat1 + geom_point(aes(x=PC1.1,y=PC2.1))
This is run on a dataset, called dflat which looks like this:
site geom PC1.1 PC2.1 PC3.1 PC1.2 PC2.2
1 Buhlen 1259.5649 -0.0387975838 -0.022889782 0.01355317 0.008705276 0.02441577
2 Buhlen 653.6607 -0.0009398704 -0.013076251 0.02898955 -0.001345149 0.03133990
The result is fine, but when I try to add the ellipse (let's say for this one site, called "Buhlen"):
> plat1 + geom_point(aes(x=PC1.1,y=PC2.1)) + geom_path(data=subset(dflat, site="Buhlen"),mapping=aes(x=ELLI(PC1.1,PC2.1)[,1],y=ELLI(PC1.1,PC2.1)[,2]))
I get an error message: "Error in data.frame(x = c(0.0977338099339815, 0.0844334944904515, 0.0697467834016782, :
arguments imply differing number of rows: 50, 211
I've managed to fix this in the past, but I cannot remember how. It seems that geom_path is relying on the same points rather than plotting new ones. Any help would be appreciated.