0
votes

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:

enter image description here

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")
1

1 Answers

0
votes

OK, I found it after viewing a couple of related posts. I needed to use shape=21 to 25 and fill the geom_point:

ggplot(data= data_f_for_fill,aes(x=eruptions, y=waiting)) +  
  geom_point(aes(fill=posterior2), colour="white", shape=21, size=3) +
  scale_fill_gradient ('posterior2', low = "red", high = "blue")