我正在尝试绘制以下数据集的直方图。这个数字图应该包含母亲年龄的每个独特组的条形图以及该组中总分娩数的相应计数:
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
我正在尝试使用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
这是一个错误:
Error: stat_bin() must not be used with a y aesthetic.
我在哪里犯错误?
答案 0 :(得分:1)
您的数据已经被分箱,因此您无法使用geom_histogram
,但未汇总,因此geom_col
不是一个明显的解决方案。您 可以<{1}}使用geom_bar
stat = 'summary'
作为摘要功能:
sum
...或者只是在你绘制之前聚合:
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)