Hướng dẫn vẽ đồ thị ANOVA hai yếu tố

Biên soạn

Duc Nguyen | Founder of tuhocr.com

Cập nhật

2024 June 06

‍ ‍ ‍ Để phóng lớn hình ảnh, bạn right-click vào hình chọn Open image in new tab.

Bước 1: Dataset df là kết quả thí nghiệm tương tác giữa tổ hợp hai yếu tố là phân bón và chế độ tưới nước đến sinh khối cây trồng.

df <- read.csv("compost.csv")
df
   fertilizer  water biomass
1   Compost A   High      14
2   Compost A   High      41
3   Compost A   High      49
4   Compost A   High      14
5   Compost A   High      35
6   Compost A   High      57
7   Compost A Medium      20
8   Compost A Medium      15
9   Compost A Medium      38
10  Compost A Medium      38
11  Compost A Medium      52
12  Compost A Medium      58
13  Compost A    Low      73
14  Compost A    Low      57
15  Compost A    Low      31
16  Compost A    Low      23
17  Compost A    Low      53
18  Compost A    Low      21
19  Compost B   High      47
20  Compost B   High      71
21  Compost B   High      21
22  Compost B   High      66
23  Compost B   High      29
24  Compost B   High      32
25  Compost B Medium      26
26  Compost B Medium      43
27  Compost B Medium      71
28  Compost B Medium      71
29  Compost B Medium      23
30  Compost B Medium      59
31  Compost B    Low      60
32  Compost B    Low      47
33  Compost B    Low      29
34  Compost B    Low      27
35  Compost B    Low      16
36  Compost B    Low      45

Bước 2: Ta chuyển các cột fertilizerwater về class factor, cũng như thực hiện một số bước thống kê mô tả dữ liệu.

df$fertilizer <- factor(df$fertilizer, levels = c("Compost A", "Compost B"))
df$water <- factor(df$water, levels = c("Low", "Medium", "High"))
table(df$fertilizer, df$water, useNA = "always") ### Tổ hợp nghiệm thức
           
            Low Medium High <NA>
  Compost A   6      6    6    0
  Compost B   6      6    6    0
  <NA>        0      0    0    0

Bước 3: Tính trung bình và độ lệch chuẩn.

df |> dplyr::group_by(fertilizer, water) |> 
  dplyr::summarize(trung_binh = mean(biomass),
                   do_lech_chuan = sd(biomass)) -> df_ok

df_ok
  fertilizer  water trung_binh do_lech_chuan
1  Compost A    Low       43.0          21.1
2  Compost A Medium       36.8          17.0
3  Compost A   High       35.0          17.9
4  Compost B    Low       37.3          16.1
5  Compost B Medium       48.8          21.5
6  Compost B   High       44.3          20.6

Bước 4: Tách ra vector trung bình và độ lệch chuẩn để vẽ error bar.

df_ok$trung_binh -> trung_binh

names(trung_binh) <- paste0(df_ok$fertilizer, "_", df_ok$water)

round(trung_binh, digits = 1)
   Compost A_Low Compost A_Medium   Compost A_High    Compost B_Low Compost B_Medium   Compost B_High 
            43.0             36.8             35.0             37.3             48.8             44.3 
df_ok$do_lech_chuan -> do_lech_chuan

names(do_lech_chuan) <- paste0(df_ok$fertilizer, "_", df_ok$water)

round(do_lech_chuan, digits = 1)
   Compost A_Low Compost A_Medium   Compost A_High    Compost B_Low Compost B_Medium   Compost B_High 
            21.1             17.0             17.9             16.1             21.5             20.6 

Bước 5: Vẽ đồ thị line chart với error bar.

# windows(8, 8, "fixed")

png(filename = "fertilizer_vs_water.png", width = 8, height = 8, units = "in", res = 300)

par(pty = "s")
par(mar = c(5, 5, 5, 5))

### plot để lấy tọa độ
interaction.plot(x.factor = df$water, 
                 trace.factor = df$fertilizer, 
                 trace.label = "Phân bón",
                 response = df$biomass, 
                 ann = FALSE,
                 axes = FALSE,
                 fun = mean,
                 type = "o", 
                 legend = FALSE,
                 xlab = bquote(bold("Chế độ tưới nước")),
                 ylab = bquote(bold("Sinh khối (g)")),
                 ylim = c(0, 100),
                 yaxs = "i",
                 pch = c(15, 16), 
                 col = c(adjustcolor( "red", alpha.f = 0),
                         adjustcolor( "blue", alpha.f = 0)),
                 las = 1,
                 cex = 2,
                 lwd = 2,
                 lty = c(1, 2),
                 font = 2)

grid(col = "lightgray")

par(lheight = 1.2)

title(main = "Kết quả thí nghiệm ảnh hưởng giữa các loại phân bón\nvà chế độ tưới nước đến sinh khối cây trồng")

par(lheight = 1.1)

legend(x = "top",
       y = NULL,
       legend = c("Compost A   ", "Compost B"),
       x.intersp = 1,
       y.intersp = 1.1,
       pch = c(15, 16),
       col = c("red", "blue"),
       lwd = 2,
       lty = c(1, 2),
       seg.len = 3,
       pt.cex = 1.5,
       inset = 0.02,
       title.adj = c(0.5),
       title = "Nghiệm thức phân bón\n(tính theo giá trị trung bình)",
       bty = "n",
       title.font = 2,
       title.col = "darkgreen",
       horiz = TRUE)

########################
# 
# points(x = c(1, 1, 1, 1, 1, 1),
#        y = subset(df, fertilizer == "Compost A" & water == "Low")$biomass,
#        col = adjustcolor( "red", alpha.f = 1),
#        pch = 0,
#        cex = 1.1)
# 
# points(x = c(1, 1, 1, 1, 1, 1),
#        y = subset(df, fertilizer == "Compost B" & water == "Low")$biomass,
#        col = adjustcolor( "blue", alpha.f = 1),
#        pch = 1,
#        cex = 1.1)
# 
# ########################
# 
# points(x = c(2, 2, 2, 2, 2, 2),
#        y = subset(df, fertilizer == "Compost A" & water == "Medium")$biomass,
#        col = adjustcolor( "red", alpha.f = 1),
#        pch = 0,
#        cex = 1.1)
# 
# points(x = c(2, 2, 2, 2, 2, 2),
#        y = subset(df, fertilizer == "Compost B" & water == "Medium")$biomass,
#        col = adjustcolor( "blue", alpha.f = 1),
#        pch = 1,
#        cex = 1.1)
# 
# ########################
# 
# points(x = c(3, 3, 3, 3, 3, 3),
#        y = subset(df, fertilizer == "Compost A" & water == "High")$biomass,
#        col = adjustcolor( "red", alpha.f = 1),
#        pch = 0,
#        cex = 1.1)
# 
# points(x = c(3, 3, 3, 3, 3, 3),
#        y = subset(df, fertilizer == "Compost B" & water == "High")$biomass,
#        col = adjustcolor( "blue", alpha.f = 1),
#        pch = 1,
#        cex = 1.1)

par(new = TRUE) ### vẽ đè lại plot để chuẩn đồ thị
interaction.plot(x.factor = df$water, 
                 trace.factor = df$fertilizer, 
                 trace.label = "Phân bón",
                 response = df$biomass, 
                 fun = mean,
                 type = "o", 
                 legend = FALSE,
                 xlab = bquote(bold("Chế độ tưới nước")),
                 ylab = bquote(bold("Sinh khối (g)")),
                 ylim = c(0, 100),
                 yaxs = "i",
                 pch = c(15, 16), 
                 col = c(adjustcolor( "red", alpha.f = 0.7),
                         adjustcolor( "blue", alpha.f = 0.7)),
                 las = 1,
                 cex = 2,
                 lwd = 2,
                 lty = c(1, 2),
                 font = 2)

### độ lệch chuẩn cho compost A
arrows(x0 = c(1, 2, 3), 
       y0 = trung_binh[1:3] + do_lech_chuan[1:3], 
       x1 = c(1, 2, 3), 
       y1 = trung_binh[1:3] - do_lech_chuan[1:3],
       angle = 90, 
       code = 3, 
       length = 0.035,
       col = adjustcolor( "red", alpha.f = 0.5),
       lwd = 1.5)

### độ lệch chuẩn cho compost B
arrows(x0 = c(1, 2, 3), 
       y0 = trung_binh[4:6] + do_lech_chuan[4:6], 
       x1 = c(1, 2, 3), 
       y1 = trung_binh[4:6] - do_lech_chuan[4:6],
       angle = 90, 
       code = 3, 
       length = 0.035,
       col = adjustcolor( "blue", alpha.f = 0.5),
       lwd = 1.5)

dev.off()


Bước 6: Vẽ đồ thị line chart và điểm dữ liệu tương ứng từng nghiệm thức.

# windows(8, 8, "fixed")

png(filename = "fertilizer_vs_water_datapoint.png", width = 8, height = 8, units = "in", res = 300)

par(pty = "s")
par(mar = c(5, 5, 5, 5))

### plot để lấy tọa độ
interaction.plot(x.factor = df$water, 
                 trace.factor = df$fertilizer, 
                 trace.label = "Phân bón",
                 response = df$biomass, 
                 ann = FALSE,
                 axes = FALSE,
                 fun = mean,
                 type = "o", 
                 legend = FALSE,
                 xlab = bquote(bold("Chế độ tưới nước")),
                 ylab = bquote(bold("Sinh khối (g)")),
                 ylim = c(0, 100),
                 yaxs = "i",
                 pch = c(15, 16), 
                 col = c(adjustcolor( "red", alpha.f = 0),
                         adjustcolor( "blue", alpha.f = 0)),
                 las = 1,
                 cex = 2,
                 lwd = 2,
                 lty = c(1, 2),
                 font = 2)

grid(col = "lightgray")

par(lheight = 1.2)

title(main = "Kết quả thí nghiệm ảnh hưởng giữa các loại phân bón\nvà chế độ tưới nước đến sinh khối cây trồng")

par(lheight = 1.1)

legend(x = "top",
       y = NULL,
       legend = c("Compost A   ", "Compost B"),
       x.intersp = 1,
       y.intersp = 1.1,
       pch = c(15, 16),
       col = c("red", "blue"),
       lwd = 2,
       lty = c(1, 2),
       seg.len = 3,
       pt.cex = 1.5,
       inset = 0.02,
       title.adj = c(0.5),
       title = "Nghiệm thức phân bón\n(tính theo giá trị trung bình)",
       bty = "n",
       title.font = 2,
       title.col = "darkgreen",
       horiz = TRUE)

########################

points(x = c(1, 1, 1, 1, 1, 1),
       y = subset(df, fertilizer == "Compost A" & water == "Low")$biomass,
       col = adjustcolor( "red", alpha.f = 1),
       pch = 0,
       cex = 1.1)

points(x = c(1, 1, 1, 1, 1, 1),
       y = subset(df, fertilizer == "Compost B" & water == "Low")$biomass,
       col = adjustcolor( "blue", alpha.f = 1),
       pch = 1,
       cex = 1.1)

########################

points(x = c(2, 2, 2, 2, 2, 2),
       y = subset(df, fertilizer == "Compost A" & water == "Medium")$biomass,
       col = adjustcolor( "red", alpha.f = 1),
       pch = 0,
       cex = 1.1)

points(x = c(2, 2, 2, 2, 2, 2),
       y = subset(df, fertilizer == "Compost B" & water == "Medium")$biomass,
       col = adjustcolor( "blue", alpha.f = 1),
       pch = 1,
       cex = 1.1)

########################

points(x = c(3, 3, 3, 3, 3, 3),
       y = subset(df, fertilizer == "Compost A" & water == "High")$biomass,
       col = adjustcolor( "red", alpha.f = 1),
       pch = 0,
       cex = 1.1)

points(x = c(3, 3, 3, 3, 3, 3),
       y = subset(df, fertilizer == "Compost B" & water == "High")$biomass,
       col = adjustcolor( "blue", alpha.f = 1),
       pch = 1,
       cex = 1.1)

par(new = TRUE) ### vẽ đè lại plot để chuẩn đồ thị
interaction.plot(x.factor = df$water, 
                 trace.factor = df$fertilizer, 
                 trace.label = "Phân bón",
                 response = df$biomass, 
                 fun = mean,
                 type = "o", 
                 legend = FALSE,
                 xlab = bquote(bold("Chế độ tưới nước")),
                 ylab = bquote(bold("Sinh khối (g)")),
                 ylim = c(0, 100),
                 yaxs = "i",
                 pch = c(15, 16), 
                 col = c(adjustcolor( "red", alpha.f = 0.7),
                         adjustcolor( "blue", alpha.f = 0.7)),
                 las = 1,
                 cex = 2,
                 lwd = 2,
                 lty = c(1, 2),
                 font = 2)

# ### độ lệch chuẩn cho compost A
# arrows(x0 = c(1, 2, 3), 
#        y0 = trung_binh[1:3] + do_lech_chuan[1:3], 
#        x1 = c(1, 2, 3), 
#        y1 = trung_binh[1:3] - do_lech_chuan[1:3],
#        angle = 90, 
#        code = 3, 
#        length = 0.035,
#        col = adjustcolor( "red", alpha.f = 0.5),
#        lwd = 1.5)
# 
# ### độ lệch chuẩn cho compost B
# arrows(x0 = c(1, 2, 3), 
#        y0 = trung_binh[4:6] + do_lech_chuan[4:6], 
#        x1 = c(1, 2, 3), 
#        y1 = trung_binh[4:6] - do_lech_chuan[4:6],
#        angle = 90, 
#        code = 3, 
#        length = 0.035,
#        col = adjustcolor( "blue", alpha.f = 0.5),
#        lwd = 1.5)

dev.off()


Bước 7: Phân tích ANOVA 2 yếu tố và phân hạng nghiệm thức được mình trình bày ở link này. Các bạn tham khảo tiếp nhé.

https://thongkesinhhoc.com/phan-tich-anova-2-yeu-to-trong-r

Tài liệu tham khảo

  1. https://www.statology.org/interaction-plot-r/