Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a data ellipse be superimposed on a ggplot2 scatterplot?
    primarykey
    data
    text
    <p>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):</p> <pre><code> [,1] [,2] [1,] 0.097733810 0.044957994 [2,] 0.084433494 0.050337990 [3,] 0.069746783 0.054891438 </code></pre> <p>I would like to superimpose a number of such ellipses for each level of a factor called 'site' on a <code>ggplot2</code> scatterplot, produced from this command:</p> <pre><code>&gt; plat1 &lt;- ggplot(mapping=aes(shape=site, size=geom), shape=factor(site)); plat1 + geom_point(aes(x=PC1.1,y=PC2.1)) </code></pre> <p>This is run on a dataset, called <code>dflat</code> which looks like this:</p> <pre><code>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 </code></pre> <p>The result is fine, but when I try to add the ellipse (let's say for this one site, called "Buhlen"):</p> <pre><code>&gt; 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])) </code></pre> <p>I get an error message: <code>"Error in data.frame(x = c(0.0977338099339815, 0.0844334944904515, 0.0697467834016782, : arguments imply differing number of rows: 50, 211</code></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload