错误:一元运算符的参数无效

时间:2017-03-10 08:28:32

标签: r ggplot2

我见过this。 我有以下数据:

female=c("hw","hw","uw","uw","w","w")
male=c("w","hw","uw","w")

我需要为男性和女性制作直方图,所以我制作了2个不同的数据。框架:

male<-data.frame(class=male)
female<-data.frame(class=female)

然后绘制hist:

library(ggplot2)

ggplot(data=male,col="green",aes(x=class))
+ geom_histogram(data=female,col="red")

但它抱怨:

  

一元运算符的无效参数

我需要制作绿色男性情节和蓝色女性情节。

1 个答案:

答案 0 :(得分:1)

如果你运行男性$ class,它会给出NULL,所以我不确定你在那里尝试做什么。也许是这些方面的事情:

df=data.frame(gender=as.factor(c(rep("female",6),rep("male",4))),
  outcome=as.character(c("hw","hw","uw","uw","w","w","w","hw","uw","w")))

library(ggplot2)

ggplot(data=df,aes(x=outcome,fill=gender)) + 
  geom_histogram(stat="count",position="dodge") + scale_fill_manual(values=c("blue","green"))