1
votes

I have a plotting problem with curves when using mixtools

Using the following R code

require(mixtools)
x <- c(rnorm(10000,8,2),rnorm(10000,18,5))
xMix <- normalmixEM(x, lambda=NULL, mu=NULL, sigma=NULL)
plot(xMix, which = 2, nclass=25)

I get a nice histogram, with the 2 normal curves estimated from the model superimposed.

The problem is with the default colours (i.e. red and green), which I need to change for a publication to be black and grey.

One way I thought to doing this was first to produce the histogram

hist(xMix$x, freq=FALSE, nclass=25) 

and then add the lines using the "curve" function.

....... but I lost my way, and couldn't solve it

I would be grateful for any pointers or the actual solution

thanks

PS. Note that there is an alternative work-around to this problem using ggplot: Any suggestions for how I can plot mixEM type data using ggplot2 but for various reasons I need to keep using the base graphics

2

2 Answers

3
votes

You can also edit the colours directly using the col2 argument in the mixtools plotting function

For example

plot(xMix, which = 2, nclass=25, col2=c("dimgrey","black"))
0
votes

giving the problem a bit more thought, I managed to rephrase the problem and ask the question in a much more direct way

Using user-defined functions within "curve" function in R graphics

this delivered two nice solutions of how to use the "curve" function to draw normal distributions produced by the mixture modelling.

the overall answer therefore is to use the "hist" function to draw a histogram of the raw data, then the "curve" function (incorporating the sdnorm function) to draw each normal distribution. This gives total control of the colours (and potentially any other graphic parameter).

And not to forget - this is where I got the code for the sdnorm function - and other useful insights

Any suggestions for how I can plot mixEM type data using ggplot2

Thanks as always to StackOverflow and the contributors who provide such helpful advice.