Search code examples
rggplot2histogramfill

Create a histogram filled using another variable in ggplot


I am working with a dataset that includes the age of some people. I am trying to create a histogram for the ages of the people with ggplot in which the colours of the bars of the histogram should depend on some predefined age intervals.

So for example imagine a dataset like this:

>X
   Age   Age2
   10    Under 14
   11    Under 14
   10    Under 14
   13    Under 14
   20    Between 15 and 25
   21    Between 15 and 25
   35    Above 25

I have tried to do something like this:

ggplot(X, aes(x = Age)) + geom_histogram(aes(fill = Age2))

But it displays the following error message:

Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

What am I doing wrong?


Solution

  • plotted with ggplot2, corrected excessive capitalization.

    age <-c(10,11,10,13,20,21,35)
    age2<-c(rep("Under 14", times=4), rep("Between 15 and 25",times=2),"Above 25")
    X<-as.data.frame(cbind(age,age2))
    X$age<-as.numeric(age)
    X
    names(X)
    summary(X)
    p<- ggplot(X, aes(x = age))+
      geom_histogram(aes(fill = age2))
    p
    

    sample output