2016-05-28 73 views
2

使用df及以下GGPLOT2:添加回歸方程和R2和情節

代碼調整自己的位置,我得到這個情節

enter image description here

感謝@ kdauria的回答this question,我添加了迴歸方程和R2,如下圖所示

source_gist("524eade46135f6348140") 
ggplot(df, aes(x= new_price, y= carat, color = cut))+ 
    stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE)+ 
    geom_point(alpha = 0.3)+ 
    facet_wrap(~clarity, scales = "free_y")+ 
    geom_smooth(method = "lm", se = F) 

enter image description here

現在,我要調整的迴歸方程和R2的位置,以便在每個所述小面(例如一個特定的地方在每個小面的右下方的「例如0.2y和0.8x)。

我試圖通過vjusthjust調整位置,但它沒有奏效。

任何建議將不勝感激。

回答

2

stat_poly_eq嘗試從包ggpmisc

library(ggpmisc) 
formula <- y ~ x 
ggplot(df, aes(x= new_price, y= carat, color = cut)) + 
    geom_point(alpha = 0.3) + 
    facet_wrap(~clarity, scales = "free_y") + 
    geom_smooth(method = "lm", formula = formula, se = F) + 
    stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
       label.x.npc = "right", label.y.npc = 0.15, 
       formula = formula, parse = TRUE, size = 3) 

返回

enter image description here

?stat_poly_eq參見其他選項來控制輸出。

+0

有沒有辦法添加p值? – aelwan

+0

請檢查這個問題http://stackoverflow.com/questions/37536950/ggplot2-add-p-values-to-the-plot – aelwan

+0

請參閱[答案](http://stackoverflow.com/a/ 35140066/3817004)以[ggplot2:在圖表中添加回歸直線方程和R2](http://stackoverflow.com/q/7549694/3817004)獲取更多詳細信息,或者聯繫作者。 – Uwe