I want to make a plot that would show distribution (histogram) of a population, consisting of Females and Males onto one geom_bar
with regard to Age.
It would be great if it looked something like this:
This is my data:
> head(Oriflame)
# A tibble: 6 x 3
Age Females Males
<chr> <dbl> <dbl>
1 14-20 11655 1014
2 20-25 23977 2372
3 25-30 22685 2243
4 30-40 42855 4761
5 40-50 32848 3649
6 50-60 18295 2261
7 60-70 11868 1467
8 70+ 5198 778
So, basically, I created two following plots:
ggplot(Oriflame, aes(x=Oriflame$Age, y=Oriflame$Females)) + geom_bar(stat = "identity")
ggplot(Oriflame, aes(x=Oriflame$Age, y=Oriflame$Males)) + geom_bar(stat = "identity")
And all I wish to do is to merge/overlap them onto one plot, with a fill/colour distinction of Females and Males. But I can't figure out how to dodge them.
The solution is going to be relatively easy: you convert the data from a wide format to a long format:
df <- reshape2::melt(Oriflame)
And set the fill in your plot to variable
, which is now encoding the sex. You then use the position = "dodge"
in the geom_col()
, which is shorthand for geom_bar(stat = "identity")
.
ggplot(df, aes(Age, value, fill = variable)) +
geom_col(position = "dodge")