1
votes

I have seen this. I have the data like:

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

I need to make a histogram for both male and female, so i made 2 different data.frame:

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

and then draw the hist:

library(ggplot2)

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

but it complains with:

invalid argument to unary operator

I need to make male plot in green and female in blue.

1

1 Answers

1
votes

If you run male$class it gives NULL so I am not sure what you are trying to do there. Perhaps something along these lines:

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"))