如何分别绘制男性和女性数据?

How do I plot male and female data separately?

我正在尝试在 facet wrap plot 中绘制每年的 Female 和 Male 数据。例如,2013 年女性有 10,949 个数据点,男性有 53,351 个数据点。这是数据示例:

 cost gender year
1 305.665 Female 2013
2 194.380 Female 2013
3 462.490 Female 2013
4 200.430 Female 2013
5 188.570 Female 2013
6 277.245 Female 2013

我整理的代码是:

library(ggplot2)
costs<-read.table("cost_data.txt",header=TRUE)
df<-data.frame(costs)
ggplot(df, aes(df$cost,color=df$gender)) + 
geom_histogram(breaks=seq(0,3000,by=20),alpha=0.2) + facet_wrap(~year)+
labs(x="Costs",y="Number of Members")

生成以下图表:

现在,如果我只是在 Excel 中绘制 2013 年直方图,binwidth 为 20,则女性情节将达到 300 计数的峰值,而男性情节将达到 1800 计数的峰值。所以我在图表中绘制的内容对我来说没有意义。它显示女性高于男性,我不确定为什么图例(或直方图)不可靠。

只需要一点指导。

对于那些不看评论的人...

# To show bars side-by-side
geom_histogram(breaks=seq(0,3000,by=20),alpha=0.2, position = "dodge")

# To have filled bars and legend keys
ggplot(df, aes(cost,fill=gender))

# In completion
library(ggplot2)
costs<-read.table("cost_data.txt",header=TRUE)
df<-data.frame(costs)
ggplot(df, aes(cost,fill=gender)) + 
geom_histogram(breaks=seq(0,3000,by=20),alpha=0.2, position="dodge") + facet_wrap(~year)+
labs(x="Costs",y="Number of Members")