如何分别绘制男性和女性数据? [英] How do I plot male and female data separately?

查看:81
本文介绍了如何分别绘制男性和女性数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试以多方面的方式绘制每年的女性和男性数据.例如,2013年女性有10,949个数据点,男性有53,351个数据点.这是数据示例:

I'm trying to plot Female and Male data for each year in a facet wrap plot. As an example, for the year 2013 there are 10,949 data points for female and 53,351 data points for male. Here's a sample of the data:

 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直方图,二进制宽度为20,则女性图将以300个计数达到峰值,而男性图将以1800计数达到峰值.因此,我在图表中绘制的内容对我来说没有任何意义.它显示女性要比男性高,而且我不确定为什么图例(或直方图)不是很可靠.

Now if I just plotted the 2013 histograms in Excel with a binwidth of 20, the female plot would peak at 300 counts and the male would peak at 1800 counts. So what I've plotted in the chart doesn't make sense to me. It shows the female higher than the male and I'm not sure why the legend (or the histograms) aren't solid.

只需要一点指导.

推荐答案

对于那些不阅读注释的人...

For those who don't read the comments...

# 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")

这篇关于如何分别绘制男性和女性数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆