How do I plot male and female data separately?

为君一笑 提交于 2019-12-11 14:08:17

问题


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

The code I put together is:

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

Which produces the following chart:

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.

Just need a little guidance.


回答1:


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


来源:https://stackoverflow.com/questions/55851821/how-do-i-plot-male-and-female-data-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!