簡體   English   中英

ggplot-使用漸變着色繪制后驗概率

[英]ggplot - plotting posterior probabilities with gradient colouring

使用下面的代碼,我將數據集分為兩個類,然后對點進行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")

好的,我在查看了幾篇相關 文章后才找到它。 我需要使用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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM