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

问题描述 投票:0回答:1

我试图在一个小平面图中绘制每年的女性和男性数据。例如,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")

产生以下图表:

enter image description here

现在,如果我只是在Excel中绘制2013年直方图,其海带宽度为20,那么女性阴谋将达到300计数,男性将达到1800计数。所以我在图表中绘制的内容对我来说没有意义。它显示女性高于男性,我不知道为什么传说(或直方图)不稳固。

只需要一点指导。

r ggplot2 facet-wrap
1个回答
1
投票

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

# 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")
© www.soinside.com 2019 - 2024. All rights reserved.