使用下面的代码,我将数据集分成两个类,然后将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))) +
这产生了下图:
这是我试图获得后部渐变着色,这是行不通的。我希望颜色褪色,让我们说从红色到蓝色 - 中间的点(可能属于两种混合物)都是紫色。
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")