2

I need to plot mean lines for each histogram on each panel. The data looks like this:

 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

and there are 66,000 values for Female from years 2013 to 2018 and 234,000 Male over the same period.

Here is the code I'm using:

 library(ggplot2)
 costs<-read.table("cost_gender_1.txt",header=TRUE)
 df<-data.frame(costs)
 p<-ggplot(df, aes(cost,fill=gender)) +
 geom_histogram(breaks=seq(0,3000,by=30), position = "dodge") +
 facet_wrap(~year) +
 labs(x="Costs",y="Number of Members")+ggtitle("All Tiers") +
 geom_hline(yintercept = mean(df$cost), color=df$gender,linetype = 
 "dashed",size=3) +
 theme(plot.title = element_text(color="black", size=14, face="bold"))

and the outputenter image description here

The problem is I don't know how to add lines for each gender in each panel. Also, the line color and size don't seem to change no matter what I put in there.

Angus
  • 355
  • 2
  • 12

0 Answers0