I want to visualize the results of a clustering (produced with protoclust{protoclust}
) by creating scater plots for each pair of variables used for classifying my data, colouring by classes and overlapping the ellipses for the 95% confidence interval for each of the classes (to inspect which elipses-classes overlap under each pair of variables).
I have implemented the drawing of the ellipses in two different ways and the resulting ellipses are different! (bigger ellipses for first implementation!) A priori they differ only in size (some diferent scaling?), as the centers and angle of axes, seem to be the similar in both. I guess I must be doing something wrong by using one of them (hope not with both!), or with the arguments.
Can anyone tell me what I am doing wrong?
Here the code for the two implementations; both are based on the answers to How can a data ellipse be superimposed on a ggplot2 scatterplot?
### 1st implementation
### using ellipse{ellipse}
library(ellipse)
library(ggplot2)
library(RColorBrewer)
colorpal <- brewer.pal(10, "Paired")
x <- data$x
y <- data$y
group <- data$group
df <- data.frame(x=x, y=y, group=factor(group))
df_ell <- data.frame()
for(g in levels(df$group)){df_ell <- rbind(df_ell, cbind(as.data.frame(with(df[df$group==g,], ellipse(cor(x, y),scale=c(sd(x),sd(y)),centre=c(mean(x),mean(y))))),group=g))}
p1 <- ggplot(data=df, aes(x=x, y=y,colour=group)) + geom_point() +
geom_path(data=df_ell, aes(x=x, y=y,colour=group))+scale_colour_manual(values=colorpal)
### 2nd implementation
###using function ellipse_stat()
###code by Josef Fruehwald available in: https://github.com/JoFrhwld/FAAV/blob/master/r/stat-ellipse.R
p2 <-qplot(data=df, x=x,y=y,colour=group)+stat_ellipse(level=0.95)+scale_colour_manual(values=colorpal)
Here is the two plots together (left graph is p1
implementation (ellipse()
):
The data are available here: https://www.dropbox.com/sh/xa8xrisa4sfxyj0/l5zaGQmXJt
Warning message: In cov.trob(cbind(data$x, data$y)) : Probable convergence failure
is this also happening when you run the code? $\endgroup$