3

I recently updated ggplot2 package and running into major issues drawing horizontal lines for averages per group using facets. I believe this post is no longer valid?

I am creating a time series graph using the following code:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
    geom_line(aes(colour="red"),lwd=1.3) +
    geom_smooth() +
    geom_line(stat = "hline", yintercept = "mean")+
    scale_x_date(labels=date_format("%b-%y"),breaks ="2 month")+
    geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+

     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
    ylab("DL Prod for All Skills")+
    ggtitle("BVG1 DL Prod for All Skills 2014-2015")+
    theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
          plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
          axis.title.x = element_blank(),
          legend.position="none")+
    facet_wrap(~Patch)

The number 1 issue is that I can no longer use the stat = "hline" in the geom_line(stat = "hline", yintercept = "mean") because it gives the following error: Error: No stat called StatHline. so therefore I changed it to:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
    geom_line(aes(colour="red"),lwd=1.3) +
    geom_smooth() +
    geom_hline(yintercept = mean(p2p_dt_SKILL_A$Prod_DL))+
    scale_x_date(labels=date_format("%b-%y"),date_breaks ="2 month")+
    geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+

     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
     geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
    ylab("DL Prod for All Skills")+
    ggtitle("BVG1 DL Prod for All Skills 2014-2015")+
    theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
          plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
          axis.title.x = element_blank(),
          legend.position="none")+
    facet_wrap(~Patch)

But this doesn't draw the horizontal line at means per Patch. It just takes the overall mean for Prod_DL See below: enter image description here

Are there any new ways now to calculate mean per group and draw horizontal lines?

Thanks

UPDATE

Here is what I did:

#first create a dataframe which holds patch and mean values for prod dl, this will then be used in geom_hline()
mean_Prod_DL <- p2p_dt_SKILL_A%>%
                                group_by(Patch)%>%
                                summarise(mean_Prod_DL_per_patch = mean(Prod_DL))


ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
        scale_x_date(labels=date_format("%b-%y"),date_breaks ="2 months")+
        geom_line(aes(colour="red"),lwd=1.3) +
        geom_smooth() +
        geom_hline(data = mean_Prod_DL,aes(yintercept = mean_Prod_DL_per_patch),lty=2)+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+
         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-12-04"]))+
        ylab("DL Prod for All Skills")+
        ggtitle("BVG1 DL Prod for All Skills 2014-2016")+
        theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
              plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
              axis.title.x = element_blank(),
              legend.position="none")+
        facet_wrap(~Patch)

enter image description here

Community
  • 1
  • 1
Shery
  • 1,808
  • 5
  • 27
  • 51
  • 2
    you could create a new dataset with yintercept for each facet – MLavoie Jan 15 '16 at 16:47
  • @MLavoie is there any better solution to draw horizontal lines for means per group? – Shery Jan 15 '16 at 16:51
  • maybe somebody will come up with a better solution. – MLavoie Jan 15 '16 at 17:13
  • I did exactly what you said but the previous version was doing it smoothly. used the example in the docs `# To show different lines in different facets, use aesthetics p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~ cyl) mean_wt <- data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00)) p + geom_hline(aes(yintercept = wt), mean_wt) ` – Shery Jan 15 '16 at 18:01
  • 2
    Another clever way from [here](http://stackoverflow.com/questions/6684549/how-can-i-overlay-by-group-plot-elements-to-ggplot2-facets?answertab=votes#tab-top), that doesnt need any calculation outside of ggplot. So just add another `geom_smooth` line .. `geom_smooth(se=FALSE, method="lm", formula=y~1, colour="black")` – user20650 Jan 15 '16 at 19:08

1 Answers1

4

I agree with @MLavoie that just calculating the quantity of interest is the simplest solution. Not sure in what way you are looking for something 'better'.

Example:

# sample data
my_df <- data.frame(x=rep(1:100, 4),
                    y=cumsum(rnorm(400)),
                    category=rep(letters[1:4], each=100))

# calculate the hline data in one line with data.table
library(data.table)
setDT(my_df)[, cat_mean := mean(y), by=category]

# plot
ggplot(my_df, aes(x=x, y=y, group=category)) +
  geom_line(color='red') +
  geom_smooth(color='blue') +
  geom_hline(aes(yintercept=cat_mean)) +
  facet_wrap(~category)

Result:

enter image description here

arvi1000
  • 9,393
  • 2
  • 42
  • 52