Madhu Sareen
Madhu Sareen

Reputation: 549

ggplot aesthetic aes() is returning error for stat_bin()

I am trying to plot a histograms of the following data set. This gistograms should have bars for each unique group for mother's age and respective counts for total births within this group:

mother_age  birth_count
25  -  29              2
30  -  34              1
35  -  39              2
40  -  44              2
20  -  24              2
25  -  29              7
30  -  34              13
35  -  39              5
40  -  44              1
15  -  19              5
20  -  24              8
25  -  29              25
30  -  34              46
35  -  39              31
40  -  44              6
15  -  19              16
20  -  24              48
25  -  29              162
30  -  34              212
35  -  39              100
40  -  44              22
15  -  19              7
20  -  24              63
25  -  29              162
30  -  34              237
35  -  39              128
40  -  44              20
15  -  19              1
20  -  24              15
25  -  29              48

I am trying to plot histograms using ggplot:

df1$mother_age <- as.factor(df1$mother_age)
df1$birth_count <- as.numeric(df1$birth_count)

mthr_chld <- ggplot(df1, aes(x=mother_age, y=birth_count)) +
  ggtitle ("Mother Children") + 
  geom_histogram() + 
  labs(x = 'Mother\'s Age Group', y = 'Total Births')

mthr_chld

It is throwing an error:

Error: stat_bin() must not be used with a y aesthetic.

Where I am doing mistakes?

Upvotes: 0

Views: 2085

Answers (1)

alistaire
alistaire

Reputation: 43334

Your data is already binned, so you can't use geom_histogram, but not aggregated, so geom_col is not an obvious solution. You could use geom_bar with stat = 'summary' with sum as the summary function:

library(ggplot2)

df <- read.table(text = 'mother_age_group    birth_count
                        "25  -  29"              2
                        "30  -  34"              1
                        "35  -  39"              2
                        "40  -  44"              2
                        "20  -  24"              2
                        "25  -  29"              7
                        "30  -  34"              13
                        "35  -  39"              5
                        "40  -  44"              1
                        "15  -  19"              5
                        "20  -  24"              8
                        "25  -  29"              25
                        "30  -  34"              46
                        "35  -  39"              31
                        "40  -  44"              6
                        "15  -  19"              16
                        "20  -  24"              48
                        "25  -  29"              162
                        "30  -  34"              212
                        "35  -  39"              100
                        "40  -  44"              22
                        "15  -  19"              7
                        "20  -  24"              63
                        "25  -  29"              162
                        "30  -  34"              237
                        "35  -  39"              128
                        "40  -  44"              20
                        "15  -  19"              1
                        "20  -  24"              15
                        "25  -  29"              48', head = T)

ggplot(df, aes(mother_age_group, birth_count)) + 
    geom_bar(stat = 'summary', fun.y = sum)

...or just aggregate before you plot:

library(dplyr)

df %>% count(mother_age_group, wt = birth_count) %>% 
    ggplot(aes(mother_age_group, n)) + 
    geom_col()

Upvotes: 1

Related Questions