Bài tập Base R Graphics

Biên soạn

Duc Nguyen | Founder of tuhocr.com

Cập nhật

2024 February 08

Bài 1 - Vẽ đồ thị đường (line chart)

Dữ liệu để vẽ đồ thị

trees -> df
names(df) <- c("duong_kinh", "chieu_cao", "the_tich")
df
   duong_kinh chieu_cao the_tich
1         8.3        70     10.3
2         8.6        65     10.3
3         8.8        63     10.2
4        10.5        72     16.4
5        10.7        81     18.8
6        10.8        83     19.7
7        11.0        66     15.6
8        11.0        75     18.2
9        11.1        80     22.6
10       11.2        75     19.9
11       11.3        79     24.2
12       11.4        76     21.0
13       11.4        76     21.4
14       11.7        69     21.3
15       12.0        75     19.1
16       12.9        74     22.2
17       12.9        85     33.8
18       13.3        86     27.4
19       13.7        71     25.7
20       13.8        64     24.9
21       14.0        78     34.5
22       14.2        80     31.7
23       14.5        74     36.3
24       16.0        72     38.3
25       16.3        77     42.6
26       17.3        81     55.4
27       17.5        82     55.7
28       17.9        80     58.3
29       18.0        80     51.5
30       18.0        80     51.0
31       20.6        87     77.0

Đồ thị cần vẽ [1a]

Code
par(pty = "s")

par(cex.axis = 1.5)
par(font.axis = 2)

par(cex.lab = 1.5)
par(col.lab = "red")

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "l",
     col = "purple",
     xlim = c(0, 30),
     ylim = c(0, 100),
     las = 1,
     xaxs = "i",
     yaxs = "i",
     xlab = "Đường kính (inch)",
     ylab = expression("Thể tích"~"("*ft^3*")"),
     lwd = 3,
     lty = 1
     )

par(lheight = 1)

title(main = "Tương quan giữa đường kính và thể tích\ncủa cây black cherry",
      cex.main = 1.5,
      col.main = "darkgreen",
      line = 1)

points(x = df$duong_kinh,
       y = df$the_tich,
       type = "p",
       col = "blue",
       bg = "yellow",
       pch = 21,
       cex = 2,
       lwd = 2)

Đồ thị cần vẽ [1b] Yêu cầu: Thay đổi background của đồ thị.

Gợi ý dùng lệnh rect() xem ở bài 14 trong chuyên đề 1+

Code
par(pty = "s")

par(bg = "gray")

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "n",
     ann = FALSE,
     axes = FALSE)

rect(xleft = par("usr")[1],
     xright = par("usr")[2],
     ybottom = par("usr")[3],
     ytop = par("usr")[4],
     col = "lightyellow",
     border = "transparent",
     lwd = 2)

par(new = TRUE)

par(cex.axis = 1.5)
par(font.axis = 2)
par(col.axis = "black")
par(cex.lab = 1.5)
par(col.lab = "black")

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "l",
     col = "purple",
     xlim = c(0, 30),
     ylim = c(0, 100),
     las = 1,
     xaxs = "i",
     yaxs = "i",
     xlab = "Đường kính (inch)",
     ylab = expression("Thể tích"~"("*ft^3*")"),
     lwd = 3,
     lty = 1
     )

par(lheight = 1)

title(main = "Tương quan giữa đường kính và thể tích\ncủa cây black cherry",
      cex.main = 1.5,
      col.main = "black",
      line = 1)

points(x = df$duong_kinh,
       y = df$the_tich,
       type = "p",
       col = "blue",
       bg = "yellow",
       pch = 21,
       cex = 2,
       lwd = 2)

Đồ thị cần vẽ [1c] Yêu cầu: Thêm đường grid line tọa độ và modify chi tiết hơn.

Code
par(pty = "s")

par(bg = "lightgreen")

par(mar = c(6, 6, 6, 6))

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "n",
     xlim = c(0, 30), # khai báo kỹ trục x, y để grid() vẽ chính xác
     ylim = c(0, 100),
     xaxs = "i",
     yaxs = "i",
     ann = FALSE,
     axes = FALSE)

rect(xleft = par("usr")[1],
     xright = par("usr")[2],
     ybottom = par("usr")[3],
     ytop = par("usr")[4],
     col = "lightyellow",
     border = "transparent",
     lwd = 2)

grid(col = "red", lty = 3)

par(new = TRUE)

par(cex.axis = 1.5)
par(font.axis = 2)
par(col.axis = "black")
par(cex.lab = 1.5)
par(col.lab = "black")


par(fg = "blue")

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "l",
     col = "purple",
     xlim = c(0, 30),
     ylim = c(0, 100),
     las = 1,
     xaxs = "i",
     yaxs = "i",
     xlab = "Đường kính (inch)",
     ylab = expression("Thể tích"~"("*ft^3*")"),
     lwd = 3,
     lty = 1,
     bty = "o" # vẽ viền xung quanh đồ thị
     )

par(lheight = 1)

title(main = "Tương quan giữa đường kính và thể tích\ncủa cây black cherry",
      cex.main = 1.5,
      col.main = "black",
      line = 1)

points(x = df$duong_kinh,
       y = df$the_tich,
       type = "p",
       col = "blue",
       bg = "yellow",
       pch = 21,
       cex = 2,
       lwd = 2)

box(which = "figure", lty = 1, col = "blue", lwd = 4)

Đồ thị cần vẽ [1d] Yêu cầu: Thể hiện đường tọa độ giữa x và y.

Để làm cho color ở các đường tọa độ trở nên transparent (giá trị alpha) thì ta dùng function sau:

col = adjustcolor("blue", alpha.f = 0.5)

Code
par(pty = "s")

par(bg = "lightgreen")

par(mar = c(6, 6, 6, 6))

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "n",
     xlim = c(0, 30), # khai báo kỹ trục x, y để grid() vẽ chính xác
     ylim = c(0, 100),
     xaxs = "i",
     yaxs = "i",
     ann = FALSE,
     axes = FALSE)

rect(xleft = par("usr")[1],
     xright = par("usr")[2],
     ybottom = par("usr")[3],
     ytop = par("usr")[4],
     col = "lightyellow",
     border = "transparent",
     lwd = 2)

grid(col = "red", lty = 3)

par(new = TRUE)

par(cex.axis = 1.5)
par(font.axis = 2)
par(col.axis = "black")
par(cex.lab = 1.5)
par(col.lab = "black")


par(fg = "blue")

plot(x = df$duong_kinh,
     y = df$the_tich,
     type = "l",
     col = "purple",
     xlim = c(0, 30),
     ylim = c(0, 100),
     las = 1,
     xaxs = "i",
     yaxs = "i",
     xlab = "Đường kính (inch)",
     ylab = expression("Thể tích"~"("*ft^3*")"),
     lwd = 3,
     lty = 1,
     bty = "o" # vẽ viền xung quanh đồ thị
     )

par(lheight = 1)

title(main = "Tương quan giữa đường kính và thể tích\ncủa cây black cherry",
      cex.main = 1.5,
      col.main = "black",
      line = 1)

points(x = df$duong_kinh,
       y = df$the_tich,
       type = "h",
       col = adjustcolor("blue", alpha.f = 0.5),
       cex = 2,
       lwd = 2)

segments(x0 = 0,
         y0 = df$the_tich,
         x1 = df$duong_kinh,
         y1 = df$the_tich,
         col = adjustcolor("blue", alpha.f = 0.5),
         cex = 2,
         lwd = 2)

points(x = df$duong_kinh,
       y = df$the_tich,
       type = "p",
       col = "blue",
       bg = "yellow",
       pch = 21,
       cex = 2,
       lwd = 2)

box(which = "figure", lty = 1, col = "blue", lwd = 4)