簡體   English   中英

ggplot geom_bar() 在繪圖上填充不着色條

[英]ggplot geom_bar() fill not coloring bars on plot

在 geom_bar 上使用 fill 參數不會為我的繪圖上的條着色。 我使用的是從泰坦尼克號數據集train.csv這里

passengers <- read.csv('../input/train.csv')

我嘗試將填充移動到 aes() 之外,嘗試將 aes 移動到 ggplot() 函數。

這是我在泰坦尼克號數據集上使用的代碼

ggplot(data = passengers) + 
    geom_bar(mapping = aes(x=Survived, fill = Pclass))

在此處輸入圖片說明

這是我用作模板的代碼,它在內置鑽石數據的 ggplot 上運行良好。

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = cut))

在此處輸入圖片說明

我只是不斷地使用 geom_bar for Survived 獲得灰色條,使用 Pclass 作為填充。

這對我有用:

ggplot(data = passengers) + 
   geom_bar(mapping = aes(x=Survived, fill = as.character(Pclass)))

在此處輸入圖片說明

你可以試試 as.factor()

ggplot(data = passengers) + 
geom_bar(mapping = aes(x=Survived, fill = as.factor(passengers$Pclass)))

可能你的變量不是因素

暫無
暫無

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

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