2017-08-10 76 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来检查数据类。 –