Add variable geom_hline to all facets of a barplot

后端 未结 2 701
悲&欢浪女
悲&欢浪女 2021-01-28 07:16

I have a barplot using the ggplot2 library:

plot <- qplot(Date, data=cns, 
              geom=\"bar\", binwidth = 1, 
              fill=Type, facets = N         


        
2条回答
  •  猫巷女王i
    2021-01-28 07:49

    You can make new column in your data frame that contains mean value. I named it as y.int and calculated using function ddply() from library plyr. Here mean value calculated only for the values where Type is Completed (as Requested should be excluded).

    library(plyr)
    cns<-ddply(cns,.(Name),transform,y.int=mean(Days[Type=="Completed"]))
    

    Now use geom_hline() and new column to add lines to each facet.

    plot + geom_hline(aes(yintercept=y.int))
    

提交回复
热议问题