Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

ggplot2 - Adding a 3rd order polynomial and its equation to a ggplot in r

I have plotted the following data and added a loess smoother. I would like to add a 3rd order polynomial and its equation (incl. the residual) to the plot. Any advice?

set.seed(1410)
dsmall<-diamonds[sample(nrow(diamonds), 100), ]
df<-data.frame("x"=dsmall$carat, "y"=dsmall$price)

p <-ggplot(df, aes(x, y)) 
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)

#Add a loess smoother
p<- p + geom_smooth(method="loess",se=TRUE)

enter image description here

How can I add a 3rd order polynomial? I have tried:

p<- p + geom_smooth(method="lm", se=TRUE, fill=NA,formula=lm(y ~ poly(x, 3, raw=TRUE)),colour="red")

Finally how can I add the 3rd order polynomial equation and the residual to the graph? I have tried:

 lm_eqn = function(df){
    m=lm(y ~ poly(x, 3, df))#3rd degree polynomial
    eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
    list(a = format(coef(m)[1], digits = 2),
    b = format(coef(m)[2], digits = 2),
    r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq))
}


data.label <- data.frame(x = 1.5,y = 10000,label = c(lm_eqn(df)))


p<- p + geom_text(data=data.label,aes(x = x, y = y,label =label), size=8,family="Times",face="italic",parse = TRUE)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Part 1: to fit a polynomial, use the arguments:

  • method=lm - you did this correctly
  • formula=y ~ poly(x, 3, raw=TRUE) - i.e. don't wrap this in a call to lm

The code:

p + stat_smooth(method="lm", se=TRUE, fill=NA,
                formula=y ~ poly(x, 3, raw=TRUE),colour="red")

enter image description here


Part 2: To add the equation:

  • Modify your functionlm_eqn() to correctly specify the data source to lm - you had a closing parentheses in the wrong place
  • Use annotate() to position the label, rather than geom_text

The code:

lm_eqn = function(df){
  m=lm(y ~ poly(x, 3), df)#3rd degree polynomial
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
                   list(a = format(coef(m)[1], digits = 2),
                        b = format(coef(m)[2], digits = 2),
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq))
}


p + annotate("text", x=0.5, y=15000, label=lm_eqn(df), hjust=0, size=8, 
             family="Times", face="italic", parse=TRUE)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...