一、載入套件與匯入資料

# 載入套件
library(haven)
library(ggplot2)
library(ggExtra)
library(ggpubr)

# 匯入 PISA 資料
PISA <- read_sav("PISA_tawian2022_trimmed_lab.sav")
# 查看資料:可手動執行,Knit 時不執行
View(PISA)

圖表繪製說明

本次使用 ggpubr 套件中的 ggscatter() 函數繪製兩組散佈圖,分別呈現「社會經濟地位與數學表現」及「年齡與社會經濟地位」之間的關係。每個點代表一位學生,依據兩個變項的數值決定其在圖中的位置。

第一張圖以社會經濟地位(ESCS)為橫軸、數學表現(PV1MATH)為縱軸,使用藍色資料點;第二張圖以年齡(AGE)為橫軸、社會經濟地位(ESCS)為縱軸,使用綠色資料點。由於資料筆數較多,因此將點的大小設為 size = 1,並透過 alpha 降低不透明度,減少資料點重疊對判讀的影響,也讓資料較密集的區域呈現較深的顏色。

兩張圖皆透過 add = "reg.line" 加入紅色線性迴歸線,以呈現整體線性趨勢,並設定 conf.int = TRUE,顯示迴歸線周圍的 95% 信賴區間。此外,使用 cor.coef = TRUE 與 cor.method = "pearson",在圖的左上角標示 Pearson 相關係數及 p 值,協助判斷線性關係的方向、強弱與統計顯著性。

最後,使用 theme_minimal() 簡化背景,將標題置中、座標軸標題加粗並移除次要格線,使圖表更清楚易讀。

二、社會經濟地位與數學表現

繪製散佈圖

ggscatter(
  PISA,
  x = "ESCS",
  y = "PV1MATH",
  color = "#3274A1",
  size = 1,
  alpha = 0.25,
  add = "reg.line",
  add.params = list(color = "#C44E52", linewidth = 1),
  conf.int = TRUE,
  cor.coef = TRUE,
  cor.method = "pearson",
  cor.coeff.args = list(
    label.x.npc = "left",
    label.y.npc = "top"
  ),
  xlab = "Socioeconomic status (ESCS)",
  ylab = "Mathematics score (PV1MATH)",
  title = "Socioeconomic Status and Mathematics Achievement"
) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    panel.grid.minor = element_blank(),
    axis.title = element_text(face = "bold")
  )

Pearson 相關分析

# 進行 Pearson 相關分析
cor.test(PISA$ESCS, PISA$PV1MATH, method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  PISA$ESCS and PISA$PV1MATH
## t = 31.952, df = 5597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3703870 0.4146987
## sample estimates:
##       cor 
## 0.3927708
# 將相關係數四捨五入至小數點後兩位
round(
  cor(PISA$ESCS, PISA$PV1MATH, method = "pearson"),
  2
)
## [1] 0.39

三、年齡與社會經濟地位

繪製散佈圖

ggscatter(
  PISA,
  x = "AGE",
  y = "ESCS",
  color = "#36856B",
  size = 1,
  alpha = 0.20,
  add = "reg.line",
  add.params = list(color = "#C44E52", linewidth = 1),
  conf.int = TRUE,
  cor.coef = TRUE,
  cor.method = "pearson",
  cor.coeff.args = list(
    label.x.npc = "left",
    label.y.npc = "top"
  ),
  xlab = "Age (years)",
  ylab = "Socioeconomic status (ESCS)",
  title = "Age and Socioeconomic Status"
) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    panel.grid.minor = element_blank(),
    axis.title = element_text(face = "bold")
  )

Pearson 相關分析

# 進行 Pearson 相關分析
cor.test(PISA$AGE, PISA$ESCS, method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  PISA$AGE and PISA$ESCS
## t = -0.33172, df = 5597, p-value = 0.7401
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.03062489  0.02176306
## sample estimates:
##          cor 
## -0.004433958
# 將相關係數四捨五入至小數點後兩位
round(
  cor(PISA$AGE, PISA$ESCS, method = "pearson"),
  2
)
## [1] 0