R語言可視化——ggplot2畫迴歸曲線

0引言

數據可視化——一文入門ggplot2中介紹了ggplot2包以及他的基本語法。在R語言可視化——ggplot2包的八種默認主題及其擴展包中介紹ggplot2包中默認的八種主題。今天實戰一下,使用ggplot2包畫迴歸曲線添加回歸方程、方差分析表,調整的R2R^2、調整迴歸數據等,本篇使用默認主題。

1、構造迴歸數據

畫圖離不開數據,下面使用生成隨機數的方式生成數據,爲了代碼的可重複性設置隨機數種子。

> n = 100
> set.seed(0)
> x <- runif(n, 0, 4)
> y <- x^2 - x + rnorm(n, 0, 0.4)
> MyClass <- factor((x>0) + (x>1) + (x>2) + (x>3),
+ labels = c("0-1", "1-2", "2-3", "3-4"))
> Data <- data.frame(x = x, y = y, class = MyClass)
> head(Data)
          x           y class
1 3.5867888  9.38472004   3-4
2 1.0620347 -0.08479814   1-2
3 1.4884956  1.70366940   1-2
4 2.2914135  2.64102651   2-3
5 3.6328312  9.54268009   3-4
6 0.8067277 -0.05586157   0-1
> str(Data)
'data.frame':   100 obs. of  3 variables:
 $ x    : num  3.59 1.06 1.49 2.29 3.63 ...
 $ y    : num  9.3847 -0.0848 1.7037 2.641 9.5427 ...
 $ class: Factor w/ 4 levels "0-1","1-2","2-3",..: 4 2 2 3 4 1 4 4 3 3 ...
> class(Data)
[1] "data.frame"

下部分使用本節的數據進行畫圖。

2、畫圖

2.1載入包

除了載入ggplot2包之外還需要載入擴展包:ggpmisc

library(ggplot2) #加載ggplot2包
library(ggpmisc) #加載ggpmisc包

2.2 準備數據添加散點

p <- ggplot(Data, aes(x, y)) +
 geom_point(color = "green",size = 2, alpha = 0.65)
p

在這裏插入圖片描述

2.3添加迴歸線

se參數控制是否顯示誤差區域。

p + 
 stat_smooth(color = "blue", formula = y ~ x, fill = "blue", method = "lm")+
 stat_fit_deviations(formula = y ~ x, color = "skyblue")

在這裏插入圖片描述

2.5 添加公式R方

注:label.x 、 label.y調整公式位置。

p + 
 stat_smooth(color = "blue", formula = y ~ x,fill = "blue", method = "lm") +
 stat_poly_eq(
   aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~~~')),
   formula = y ~ x,  parse = TRUE,
     size = 4, # 公式字體大小
     label.x = 0.1,
     label.y = 0.8)

在這裏插入圖片描述

2.6 添加方差分析表

p + 
 stat_smooth(color = "blue", formula = y ~ x,fill = "blue", method = "glm") +
 stat_poly_eq(
   aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~~~')),
   formula = y ~ x,  parse = TRUE,
     size = 4, #公式字體大小
     label.x = 0.1,
     label.y = 0.8) + 
 stat_fit_tb(method = "lm",
             method.args = list(formula = y ~ x),
             tb.type = "fit.anova",
             tb.vars = c(Effect = "term",
                         "自由度" = "df",
                         "均方" = "meansq",
                         "italic(F值)" = "statistic",
                         "italic(P值)" = "p.value"),
             label.y = 0.7, label.x = 0.05,
             size = 4.5,
             parse = TRUE
)

在這裏插入圖片描述

2.6 迴歸數據調整

模型的參數形式可以通過I函數去調整,如下把一次數據換成二次的數據。

p + 
 stat_smooth(color = "blue", formula = y ~ I(x^2),fill = "blue", method = "glm") +
 stat_poly_eq(
   aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~~~')),
   formula = y ~ I(x^2),  parse = TRUE,
     size = 4, #公式字體大小
     label.x = 0.1,
     label.y = 0.8) + 
 stat_fit_tb(method = "lm",
             method.args = list(formula = y ~ I(x^2)),
             tb.type = "fit.anova",
             tb.vars = c(Effect = "term",
                         "自由度" = "df",
                         "均方" = "meansq",
                         "italic(F值)" = "statistic",
                         "italic(P值)" = "p.value"),
             label.y = 0.7, label.x = 0.05,
             size = 4.5,
             parse = TRUE
)

在這裏插入圖片描述
上面可以看出R2R^2和迴歸的效果變得更好了。

3、總結

上面的函數侷限是隻能展示一元迴歸的信息,多元迴歸的展示方式下次推出。關注持續關注。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章