2017-08-10 138 views
1

我試圖提高我在R語言上的技能,並且發現了一個問題。我無法正確繪製一個geom_bar

#Load the library. 
library(ggplot2) 

#Execute a simple code 
ggplot(mtcars, aes(x = cyl, fill = am)) + geom_bar() 

I can´t post the image click here

我的主要問題是,我在做什麼不好,爲什麼填充審美尚未繪製

+0

爲了讓您一開始,看看' ggplot(mtcars,aes(x = cyl,fill = as.factor(am)))+ geom_bar()' – SymbolixAU

回答

2

阿德里安。在你使用它的方式中,使用geom_bar(),填充應該是一個因子而不是一個連續變量。

ggplot(mtcars, aes(x = cyl, fill = as.character(am))) ## as.character or as.vector transform "am" 
     + geom_bar() 

enter image description here

要ilustate在載體和數字之間ggplot的行爲differenece,看看這個情節:

ggplot(mtcars, aes(x = cyl, fill = as.character(am), color = as.character(am), alpha = am)) 
     + geom_bar() 

enter image description here

+0

是的,我上傳了錯誤的。編輯帖子。 –

+0

非常感謝,千萬不要通過mi mind來檢查數據類。 –