---
title: "Bài tập Base R Graphics"
author-title: <b>Biên soạn</b>
author: Duc Nguyen | Founder of <a href="https://www.tuhocr.com/">tuhocr.com</a>
site-url: https://www.tuhocr.com/
published-title: <b>Cập nhật</b>
date: last-modified
date-format: "YYYY MMMM DD"
format:
html:
page-layout: full
code-fold: true
code-tools: true
embed-resources: true
---
# [Bài 1 - Vẽ đồ thị đường (line chart)]{style="color:red"}
**Dữ liệu để vẽ đồ thị**
```{r}
#| code-fold: false
#| code-summary: "Show the code"
trees -> df
names(df) <- c("duong_kinh", "chieu_cao", "the_tich")
df
```
**Đồ thị cần vẽ** [**[1a]**]{style="color:red"}
```{r, fig.width=8, fig.height=8}
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]**]{style="color:red"} **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+](https://www.tuhocr.com/r-courses/r-crash-course)**
```{r, fig.width=8, fig.height=8}
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]**]{style="color:red"} **Yêu cầu: Thêm đường grid line tọa độ và modify chi tiết hơn.**
```{r, fig.width=8, fig.height=8}
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]**]{style="color:red"} **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)`**
```{r, fig.width=8, fig.height=8}
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)
```