2

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?

2
  • 3
    From the error message it seems like x (Age) isn't continuous. Try to change Age to as.numeric(Age). Commented Jan 16, 2017 at 9:34
  • 2
    Or as.numeric(as.character(Age)) if Age is a factor!! See str(X). Commented Jan 16, 2017 at 10:29

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.