2017-06-01 35 views
0

我使用下面的代碼忽略傳奇冠軍,但我的指揮實驗室(填寫=「類」)應該設置圖例標題爲「類」被忽略:實驗室()命令來設置由ggplot

ggplot(data_Test, aes(x = predictions_plain, fill = as.factor(income_Class), 
color = as.factor(income_Class))) + 
     geom_histogram(aes(y=..density..), position="identity",alpha = 0.4) + 
guides(color = FALSE) 
     geom_density (alpha = 0.5)+ 
    labs(title = "Predicted Probabilities per Class in the Test Dataset", 
     x = "Predicted Probability of being in Class1", y = "Count") + 
     labs(fill = "Class") 

enter image description here

您的建議將不勝感激。

+0

爲什麼有兩個獨立的'實驗室()'電話?你有沒有嘗試將'fill ='Class''移動到與其他標題相同的調用中? – Marius

+1

另外我覺得你在'guides(color = FALSE)'後面缺少'+'。這意味着後面的所有命令都不會應用於繪圖。 – Marius

回答

1

編輯:我認爲2 labs()和其他語法錯誤是你的問題。

ggplot(data_Test, aes(x = predictions_plain)) + 
    geom_histogram(aes(y=..density.., fill = as.factor(income_Class)), 
       position = "identity", alpha = 0.4) + 
    geom_density(alpha = 0.5, aes(color = as.factor(income_Class))) + 
    guides(color = FALSE) + 
    labs(title = "Predicted Probabilities per Class in the Test Dataset", 
     x = "Predicted Probability of being in Class1", 
     y = "Count", 
     fill = "Class") 

而這裏的另一種方式......

ggplot(data_Test, aes(x = predictions_plain)) + 
    geom_histogram(aes(y=..density.., fill = as.factor(income_Class)), 
       position = "identity", alpha = 0.4) + 
    geom_density(alpha = 0.5, aes(color = as.factor(income_Class))) + 
    guides(color = FALSE) + 
    labs(title = "Predicted Probabilities per Class in the Test Dataset", 
     x = "Predicted Probability of being in Class1", 
     y = "Count") + 
    scale_fill_discrete(name = "Class") 
+0

'?labs'表明這是設置圖例標題的好方法,我總是這樣做。它說:「您還可以在單​​個刻度中設置軸和圖例標籤(使用第一個參數,名稱,如果您要更改其他刻度選項,我建議您這樣做*」 – Marius

+0

是的,您說的沒錯,實驗室(填充=「類」)應該工作,如果其餘的語法是正確的。我一直這樣做:) – neilfws