1

I am working with a data frame in R that looks like this:
NEI_BaltLaVeh

'data.frame':   6497651 obs. of  6 variables:
 $ fips     : chr  "09001" "09001" "09001" "09001" ...
 $ SCC      : chr  "10100401" "10100404" "10100501" "10200401" ...
 $ Pollutant: chr  "PM25-PRI" "PM25-PRI" "PM25-PRI" "PM25-PRI" ...
 $ Emissions: num  15.714 234.178 0.128 2.036 0.388 ...
 $ type     : chr  "POINT" "POINT" "POINT" "POINT" ...
 $ year     : int  1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 ...

Using ggplot I am looking at the total emissions(using the "sum" feature in stat_summary) over time for two different areas(different "fips" codes) using:

g<-ggplot(NEI_BaltLaVeh, aes(year, Emissions, colour=fips))
g+ stat_summary(fun.y="sum", geom="point")

This works fine. However, I would also like to apply the geom_smooth linear fit function to these to juxtaposed data sets. Its clear that just calling:

g+ stat_summary(fun.y="sum", geom="point")+geom_smooth(method="lm")

applies geom_smooth to the original data set sent to ggplot before it was transformed by stat_summary giving me this plot:

Plot with incorrect geom_smooth

Is there an elegant way for me to apply geom_smooth to the transformed data without having to go back and manipulate the data?

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • 2
    Maybe something like `stat_summary(fun.y = "sum", geom = "smooth")`? Read [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to see ideas on how to make a reproducible example when asking a question. – aosmith Nov 06 '17 at 22:42
  • 1
    I realize that I just gave a snippet of the data set but I assumed that the question would be straight-forward enough without anyone having to reproduce the code on their own machines. My first time on stackoverflow re: R so I'll get better at the customs. – Musicpulpite Nov 06 '17 at 22:53
  • Including the geom="smooth" in stat_summary connects the new points but doesn't give a line of best fit. – Musicpulpite Nov 06 '17 at 22:54
  • Yeah, I don't see how to pass `method` to `stat_summary` (although it seems like it should work). You can always do the summarizing outside of ggplot2 and then plot the summarized data. – aosmith Nov 06 '17 at 22:59
  • That's probably what I'll end up doing. Oh well. Thanks. – Musicpulpite Nov 06 '17 at 23:31

0 Answers0