0

I am trying to add the mean of Duration Average column in a faceted plot. Somehow I can get around it, I tried some functions such as geom_hline, but somehow it is not working on a faceted chart:

added this block to the chart, but it did not work:

geom_hline(yintercept= mean(`Duration Average`),
             linetype="dashed",
             color="black")

Create sample dataframe:

 df <- structure(list(Year_Month = c(
    "2016_06", "2016_06", "2016_07", 
    "2016_07", "2016_08", "2016_08", "2016_09", "2016_09", "2016_09", 
    "2016_09", "2016_10", "2016_10", "2016_10", "2016_10", "2016_11", 
    "2016_11", "2016_12", "2016_12", "2017_01", "2017_01", "2017_01", 
    "2017_02", "2017_02", "2017_02", "2017_02", "2017_03", "2017_03", 
    "2017_03", "2017_03", "2017_03", "2017_03", "2017_04", "2017_04", 
    "2017_04", "2017_04", "2017_04", "2017_05", "2017_05", "2017_05", 
    "2017_05", "2017_05", "2017_05", "2017_06", "2017_06", "2017_06", 
    "2017_06", "2017_06", "2017_06", "2017_07", "2017_07"), 
    Business = c("A", 
     "E", "A", "B", "B", "E", "F", "A", "H", "B", "A", "D", "B", "E", 
     "B", "E", "F", "B", "F", "B", "E", "A", "B", "C", "E", "F", "A", 
     "G", "D", "B", "E", "F", "A", "G", "B", "E", "F", "A", "D", "B", 
     "C", "E", "F", "A", "D", "B", "C", "E", "F", "A"),
    `MMR Count` = c(2L, 
      1L, 1L, 7L, 2L, 1L, 1L, 3L, 1L, 5L, 1L, 1L, 4L, 1L, 8L, 4L, 1L, 
      4L, 2L, 2L, 2L, 3L, 8L, 1L, 2L, 1L, 7L, 1L, 4L, 9L, 2L, 4L, 10L, 
      2L, 15L, 7L, 4L, 27L, 2L, 14L, 1L, 6L, 9L, 31L, 5L, 14L, 1L, 
      4L, 5L, 21L),
     `Duration Average` = c(37, 20, 9, 8, 2, 5, 1, 1, 
      1, 14, 1, 19, 8, 1, 21, 77, 1, 18, 8, 1, 1, 194, 9, 14, 19, 1, 
      10, 1, 6, 9, 18, 4, 12, 170, 7, 35, 9, 10, 7, 12, 3, 15, 5, 9, 
      10, 10, 18, 11, 16, 14)), .Names = c("Year_Month", "Business", 
      "MMR Count", "Duration Average"), row.names = c(NA, 50L), class = "data.frame")

Code to create the plot:

df$date = as.Date(paste0(df$Year_Month, "_01"), format = "%Y_%m_%d")


ggplot(df,
    aes(x=date,
        y=`Duration Average`,
        group=Business,
        color=Business,
        size=`MMR Count`)) +
  geom_line(aes(group=Business),stat="identity", size=1, alpha=0.7) +
  geom_point(aes(colour=Business, alpha=0.7)) +
  facet_wrap(~ Business, ncol=2) +
  scale_y_log10( limits=c(.1,1000),breaks=c(1,10,100,1000)) +   
  scale_alpha_continuous(range = c(0.5,1), guide='none') + 
  geom_text(aes(label=`Duration Average`,vjust=-1),size=3) +
  geom_text(aes(label=`MMR Count`,vjust=2),size=3,color="brown")
Ibo
  • 4,081
  • 6
  • 45
  • 65
  • @PoGibas in that post the mean is calculated beforehand while I am trying to do that simply on the fly while building the plot itself – Ibo Jul 13 '18 at 20:13
  • 2
    Just calculate it beforehand - don't rely too heavily on ggplot to do your data manipulation for you. It's a graphics package, not a data manipulation package. – Gregor Thomas Jul 13 '18 at 20:18
  • I tried that method and it worked, but it was not the approach I wished, anyway thanks! – Ibo Jul 13 '18 at 20:21
  • Working approaches are the best approaches. – Gregor Thomas Jul 13 '18 at 20:22
  • @lbo I added another answer to that question which shows that you can create your own stat layer to do the calculation for you. You can use it in your case with `+ stat_mean_line(size=1)` – MrFlick Jul 13 '18 at 20:26

0 Answers0