将一个三阶多项式及其方程添加到r中的一个ggplot中 [英] Adding a 3rd order polynomial and its equation to a ggplot in r

查看:389
本文介绍了将一个三阶多项式及其方程添加到r中的一个ggplot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绘制了以下数据并添加了一个更平滑的黄土。我想添加一个三阶多项式及其方程(包括残差)到图中。任何建议?

  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,color =black,size = 5)

#黄土更平滑
p< - p + geom_smooth(method =loess,se = TRUE)



我怎么能添加一个三阶多项式?我试过:

  p < -  p + geom_smooth(method =lm,se = TRUE,fill = NA, = lm(y〜poly(x,3,raw = TRUE)),color =red)

最后,我该如何将三阶多项式方程和残差添加到图中?
我试过了:

pre $ lt; code> lm_eqn = function(df){
m = lm(y〜poly x,3,df))#3次多项式
eq < - 替代(italic(y)== a + b%。%italic(x)*,〜italic(r)^ 2〜 =〜r2,
列表(a =格式(coef(m)[1],digits = 2),
b =格式(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)


解决方案

b

  • method = lm - 您做得正确
  • formula = y〜poly(x,3,raw = TRUE) - 即不要在调用 lm



代码:

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





第二部分:添加方程式:




  • 修改您的函数 lm_eqn()以正确指定数据源为 lm - 您在错误位置有一个右括号
  • 使用 annotate()来定位标签,代码:

      lm_eqn = function(df){
    m = lm(y〜poly(x,3),df)#3次多项式
    eq < - 替代(italic(y)== a + b%。%italic(x)*,~~ italic(r)^ 2〜=〜r2,
    list(a = format (m)[2],digits = 2),
    r2 = format(summary(m)$ r.squar ed),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)


    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)
    

    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)
    

    解决方案

    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")
    


    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)
    

    这篇关于将一个三阶多项式及其方程添加到r中的一个ggplot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆