# [R] ellipse in pairs plot

Kaveh Vakili kaveh.vakili at ulb.ac.be
Sat Nov 15 00:28:42 CET 2008

```
Dear list moderator,

I'm sending you a solution i found to the question i asked the list yesterday (i have not been able to figure out how to post it to the list directly).

elly<-function(w,col=array(1,dim(w)),pch=array(1,dim(w))){
p<-dim(w)
op<-par(mfrow=c(3,3),mar=c(0,0,0,0)+1,oma=c(1,1,1,1))
c1<-rep(1:p,p)
c2<-sort(c1)
dan<-covMcd(w)
for (i in 1:p^2) {
if(c1[i]!=c2[i]){
plot(w[,c(c1[i],c2[i])],xlab="",ylab="",xaxt="n",yaxt="n",main="",col=col,pch=pch)
grid()
ava<-c(dan\$cov[c1[i],c1[i]],dan\$cov[c1[i],c2[i]],dan\$cov[c1[i],c2[i]],dan\$cov[c2[i],c2[i]])
ellipse(mu=dan\$center[c(c1[i],c2[i])],sigma=matrix(ava,2,2),col=max(col)+1)
} else {
plot(density(w[,c2[i]],from=min(w[,c2[i]]),to=max(w[,c2[i]])),xlab="",ylab="",xaxt="n",yaxt="n",main="",xlim=range(w[,c2[i]]),type="h",col="light grey")
}
if(c1[i]!=c2[i]){
if(c1[i]==1){
axis(2)
}
if(c2[i]==1){
axis(3)
}
if(c1[i]==p){
axis(4)
}
if(c2[i]==p){
axis(1)
}
}
if(i==1){
axis(3)
}
if(i==p^2){
axis(1)
}
}
par(op)
}

>Hi dear list,
>
>I'm scrambling with this:
>
>i have a p-dimensional data frame for which i want to have a pairs() plot, with ellipse added in each lower.panel.
>
>I want the ellipse's parameter (mu, sigma) to be extracted from the appropriate entries in a pre-computed matrix ("dab" in the code below).
>
>Can it be done ?
>
>
>
>melly<-function (x, y, col = par("col"), bg = NA, pch = par("pch"),cex = 1){
>    points(x, y, pch = pch, col = col, bg = bg, cex = cex)
>    ok <- is.finite(x) & is.finite(y)
>    if (any(ok))
>        ellipse(mu=dab\$center[?],sigma=dab\$cov[?,?])
>}
>
>pairs(wf[,1:5],diag.panel=bagwag,col=wc,pch=wp,cex=.5,lower.panel=melly)
>
>
> no attachments  have been sent
>
>

```