-2

I would like to make a histogram for my data but I would also like to visualize it in such a way that each category is coloured differently but stacked together.

This is what I'm trying to achieve: Stacked histogram from already summarized counts using ggplot2

but I'm unsure how to do it for my data set and my R skills are very much on the rusty side.

My data is formatted like this

Name Category Age Year
1     A       3   2017
2     B       6   2016
3     B       12  2017
4     B       8   2017

I'm only interested in Category B so I made a subset called catB. I would like the histogram to graph the frequency of the different ages, and I would like to colour the stacks based on year (in my data there are 5 year options).

I would appreciate any help! Thank you!

2 Answers2

0
ggplot(catB, aes(x = Age, fill = Year)) +
  geom_histogram()
Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
0

one more nice graphical option. You have to add frequency(count): in example given it is count=1. However you have to see on real data what is count value:

catB <- cbind(catB, count=1)
ggplot(catB, aes(x=Age, y=count)) + geom_histogram(aes(fill=Year), stat="identity", group=1)
Nar
  • 648
  • 4
  • 8