使用下面的代码,我将数据集分成两个类,然后ggplot
是点,将每个类标记为 1 类或 2 类。
问题:如何改用渐变着色来显示data_f.k2$posterior
属于第 2 类的每个点的后验概率 ?在问题的底部,我分享了我在 using 方面的尝试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")