Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:41:35+00:00 2026-06-09T14:41:35+00:00

I have plotted the following data and added a loess smoother. I would like

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:41:36+00:00Added an answer on June 9, 2026 at 2:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large data.frame displaying some weird properties when plotted. I'd like to
I would like to smooth a time curve, that I have plotted, by applying
I have a question. I have plotted a graph using Matplotlib like this: from
I am trying to have two data series plotted in one graph as boxes
I have the following range of numpy data (deltas of usec timestamps): array([ 4.312,
I have a method which returns an NSArray with points to be plotted. Following
I have a bunch of data in a text file like this: 99150, 2012-05-18
I have large amount of data to be plotted on iPad using a core
I have a data point with a y-value of 1 being plotted, the MS
I have data like this: Date 1st var 2nd var .... nth var benchmark

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.