Search code examples
rggplot2loess

Can I export the result from a loess regression out of R?


I have performed a loess regression on some data and plotted it. The problem is I'd like to export the black line and light red line (see figure) into excel. Is it possible?

Clarification:

I want to export the underlying data from the loess regression not the graph.

enter image description here

Code used to calculate it:

ggplot(data, aes(x=bigangle, y=meanz, colour=treatment)) + 
  geom_point(data=df, aes(y = X2/median(df$X2), x=X8),color="red",alpha=.6) +
  geom_smooth(data=df, aes(y = X2/median(df$X2),x=X8),fill="red", colour="black", size=1,alpha=0.4)+
  geom_point(position=position_dodge(0.1),aes(shape=treatment),   # Shape depends on cond
             size = 4,colour="black",fill="black") 

Solution

  • I am not sure what loess function ggplot2 uses, but here is one way to extract a loess object to graph in Excel:

    # fake data
    myData <- data.frame("x"=1:100, "y"=rnorm(100))
    # loess object
    my.loess <- loess(y~x, data=temp)
    # get SE
    myPred <- predict(my.loess, se=T)
    my.output <- data.frame("fitted"=myPred$fit, "SE"=myPred$se.fit)
    
    # write out data
    write.csv(my.output, file=<path/filename>.csv)