Adding R^2 on graph with facets

后端 未结 3 469
一整个雨季
一整个雨季 2020-12-29 12:06

I am plotting geom_points on multiple facets and would like to annotate R^2 on each facet (preferably on the facet_label rather on the graph.) I have found some code here wh

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 12:34

    The following seems to work

      lm_eqn = function(df){
        m = lm(ln_x ~ ln_y, data=df)
        eq <- substitute(~~R^2~"="~r2, 
                          list(r2 = format(summary(m)$r.squared, digits = 3)))
        c(eq = as.character(as.expression(eq)));                 
     }
    

    Create new data frame containing R^2 for each roi_size

    labeldata <- ddply(df,.(roi_size),lm_eqn )
    

    and geom_text becomes

    geom_text(data=labeldata,aes(x=1.5,y=2.2,label=eq,family="serif"), color='blue',  parse=TRUE)
    

提交回复
热议问题