ggplot - 用渐变着色绘制后验概率

时间:2015-03-22 17:34:03

标签: r ggplot2 probability mixture-model

使用下面的代码,我将数据集分成两个类,然后将ggplot分为两个点,每个颜色标记为1级或2级。

问题:我如何使用渐变着色来显示属于第2类的每个点的后验概率data_f.k2$posterior?在问题的底部,我分享了使用scale_colour_gradient的尝试,这不起作用。

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))) + 

这产生了下图:

enter image description here

这是我试图获得后部渐变着色,这是行不通的。我希望颜色褪色,让我们说从红色到蓝色 - 中间的点(可能属于两种混合物)都是紫色。

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 个答案:

答案 0 :(得分:0)

好的,我在查看了几个related posts后找到了它。我需要使用shape = 21到25并填充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")