0

使用下面的代码,我将数据集分成两个类,然后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")
4

1 回答 1

0

好的,我是在查看了几篇相关 帖子后才找到的。我需要使用 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")
于 2015-03-22T18:36:58.577 回答