Search code examples
rggplot2cluster-analysisaxis-labelsdrawellipse

R: Change axis label in plot.Mclust AND/OR plot uncertainty of mclust model with ggplot2


I am really confused. I would like to change the axis labels of a plot (classification or uncertainty) for a 'Mclust' model object in R and I don't understand why it's working for a simple object with just two variables, but not several ones.

Here an example:

require(mclust)

mod1 = Mclust(iris[,1:2])
plot(mod1, what = "uncertainty", dimens = c(1,2), xlab = "test")
# changed x-axis-label

mod2 = Mclust(iris[,1:4])
plot(mod2, what = "uncertainty", dimens = c(1,2), xlab = "test")
# no changed x-axis-label

Another way I tried was with coordProj:

coordProj(data= iris[, -5], dimens = c(1,2), parameters = mod2$parameters,
          z = mod2$z, what = "uncertainty", xlab = "test")
# Error in plot.default(data[, 1], data[, 2], pch = 19, main = "", xlab = xlab,  : 
#                       formal argument "xlab" matched by multiple actual arguments

So I thought, maybe it will work with ggplot2 (and that would be my favourite option). Now I can change the axis labels and so on but I don't know how to plot the ellipses?

require(ggplot2)

ggplot(data = iris) +
  geom_point(aes(x  = Sepal.Length, y = Sepal.Width, size = mod2$uncertainty)) +
  scale_x_continuous(name = "test")

It would be nice, if someone might know a solution to change the axis labels in plot.Mclust or to add the ellipses to ggplot. Thanks a lot!


Solution

  • I started to look at the code for plot.Mclust, but then I just used stat_ellipse and changed the level until the plots looked the same. It appears to be a joint t-distribution (the default) at 50% confidence (instead of the default 95%). There's probably a better way to do it using the actual covariance matrix (mod2$parameters$variance$sigma), but this gets you to where you want.

    require(dplyr)
    
    iris %>%     
         mutate(uncertainty = mod2$uncertainty,
                classification = factor(mod2$classification)) %>% 
         ggplot(aes(Sepal.Length, Sepal.Width, size = uncertainty, colour = classification)) +
           geom_point() + 
           guides(size = F, colour = F) + theme_classic() +
           stat_ellipse(level = 0.5, type = "t") + 
           labs(x = "Label X", y = "Label Y")
    

    output of codeblock