With the code below, I split a dataset into two classes and then ggplot
the points, colour-labelling each as class 1 or 2.
Question: How can I instead use a gradient colouring to show the posterior probability, data_f.k2$posterior
, of each point belonging to class 2? At the bottom of the question, I share my attempt in using scale_colour_gradient
, which does not work.
if(!require("mixtools")) { install.packages("mixtools"); require("mixtools") }
data_f <- faithful
# fit gaussian mixture model
data_f.k2 = mvnormalmixEM(as.matrix(data_f), k=2, maxit=100, epsilon=0.01)
faithful.classes <- apply(data_f.k2$posterior,1,which.max)
# ggplot maximum a posteriori estimations per observation
map_mixtures <- ggplot(data= data_f, aes(x=eruptions, y=waiting)) +
geom_point( aes(colour=factor(faithful.classes))) +
This produces the figure below:
This is my attempt to get the posterior gradient colouring, which does not work. I want the colour to fade, let's say from red to blue - with the points in the middle (which may belong to both mixtures) as purple.
data_f$posterior2 <- data_f.k2$posterior[,'comp.2']
ggplot(data= data_f,aes(x=eruptions, y=waiting)) +
geom_point(aes(fill=posterior2), colour="grey80" ) +
scale_fill_gradient ('posterior2', low = "red", high = "blue")