0

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!

user5514978
  • 374
  • 5
  • 12

1 Answers1

2

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

Brian
  • 7,900
  • 1
  • 27
  • 41
  • 1
    Thanks for your help! Unfortunately I can't reproduce your plot, maybe you computed another mod2? But I came to the same solution, the use of `stat_ellipse`, yesterday, for my real data the option `level = 0.4, type = "norm"` seems nearest to the original plot.Mclust. Perhaps it's a little bit more senseful, when considering the model assumptions of mclust (multivariat gaussian for the components in the finite mixture model). Anyway, thanks for your effort, it's an easy way for transferring the plot. Also I had a look [here](https://groups.google.com/forum/#!topic/ggplot2/z-j_IeACczA). – user5514978 Apr 12 '17 at 07:19