Tham số las (trong lệnh par) viết tắt [L]abel of [A]xis [S]tyle

Nếu bạn muốn làm ảnh GIF như mình thì click vào chỗ Show/Hide Code để xem script template nhé.

library(animation) # tạo file gif từ đồ thị
library(unikn) # thêm textbox chú thích cho plot
ani.record(reset = TRUE) # clear history before recording

oldpar <- par(no.readonly = TRUE)

par(cex = 1.5, font = 2)

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 0, main = "las = 0 \nalways parallel to the axis [default]",
     xlab = "Trục hoành",
     ylab = "Trục tung")
mark(labels = "plot(x, y, las = 0)", x = 2012, y = 9000, cex = 0.7, col_bg = "#faff7f", col = "red")
ani.record()

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 1, main = "las = 1 \nalways horizontal",
     xlab = "Trục hoành",
     ylab = "Trục tung")
mark(labels = "plot(x, y, las = 1)", x = 2012, y = 9000, cex = 0.7, col_bg = "#faff7f", col = "red")
ani.record()

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff", 
     las = 2, main = "las = 2 \nalways perpendicular to the axis",
     xlab = "Trục hoành",
     ylab = "Trục tung")
mark(labels = "plot(x, y, las = 2)", x = 2012, y = 9000, cex = 0.7, col_bg = "#faff7f", col = "red")
ani.record()

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",  
     las = 3, main = "las = 3 \nalways vertical",
     xlab = "Trục hoành",
     ylab = "Trục tung")
mark(labels = "plot(x, y, las = 3)", x = 2012, y = 9000, cex = 0.7, col_bg = "#faff7f", col = "red")
ani.record()

saveGIF(ani.replay(), 
        interval = 2,
        movie.name = 'las.gif', ani.width = 600, ani.height = 600)

par(oldpar)

Tham số mgp (trong lệnh par)

The margin line (in mex units) for the axis title, axis labels and axis line. Note that mgp[1] affects title whereas mgp[2:3] affect axis. The default is c(3, 1, 0).

Chú ý là mgp nhận 1 vector có 3 tham số, tương ứng mỗi tham số sẽ tác động lên khoảng cách giữa tên trục (xy label) với ký hiệu (axis label) và trục đồ thị (axis).

# library(animation)
# library(unikn) 
ani.record(reset = TRUE) # clear history before recording

oldpar <- par(no.readonly = TRUE)

par(cex = 1.5, font = 2)

#### plot 1

par(mgp = c(3, 1, 0)) # default value

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 0, main = "par(mgp = c(3, 1, 0)) \ndefault value",
     xlab = "Trục hoành",
     ylab = "Trục tung")
mark(labels = "par(mgp = c(3, 1, 0)) \nplot(x, y)", x = 2012, y = 8000, cex = 0.6, col_bg = "#faff7f", col = "red")
ani.record()

#### plot 2

par(mgp = c(1, 1, 0)) 

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 0, 
     xlab = "Trục hoành",
     ylab = "Trục tung")

title(main = expression(
  bold("par(mgp = c(")
  * bold(phantom("1")) 
  * bold(", 1, 0))")
  ),
      col.main = "black", line = 2.5)

title(main = expression(
  bold(phantom("par(mgp = c(")) 
  * bold("1") 
  * bold(phantom(", 1, 0))"))
  ),
      col.main = "red", line = 2.5, font = 2)

title(main = "\nthay đổi khoảng cách giữa xy label và axis",
      col.main = "darkgreen")

mark(labels = "par(mgp = c(1, 1, 0)) \nplot(x, y)", x = 2012, y = 8000, cex = 0.6, col_bg = "#faff7f", col = "red")
ani.record()

#### plot 3

par(mgp = c(3, 2, 0)) 

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 0, 
     xlab = "Trục hoành",
     ylab = "Trục tung")

title(main = expression(
  bold("par(mgp = c(3,")
  ~ bold(phantom("2")) 
  * bold(", 0))")
  ),
      col.main = "black", line = 2.5)

title(main = expression(
  bold(phantom("par(mgp = c(3,")) 
  ~ bold("2") 
  * bold(phantom(", 0))"))
  ),
      col.main = "red", line = 2.5, font = 2)

title(main = "\nthay đổi khoảng cách giữa axis label và axis",
      col.main = "darkgreen")

mark(labels = "par(mgp = c(3, 2, 0)) \nplot(x, y)", x = 2012, y = 8000, cex = 0.6, col_bg = "#faff7f", col = "red")
ani.record()

#### plot 4

par(mgp = c(3, 2, 1)) 

plot(x = 2011:2020, y = seq(from = 1000, to = 10000, length.out = 10), pch = 19, col = "#0000ff",
     las = 0,
     xlab = "Trục hoành",
     ylab = "Trục tung")

title(main = expression(
  bold("par(mgp = c(3, 2,") ~ 
    bold(phantom("1")) 
  * bold("))")
  ),
      col.main = "black", line = 2.5)

title(main = expression(
  bold(phantom("par(mgp = c(3, 2,")) 
  ~ bold("1") 
  * bold(phantom("))"))
  ),
      col.main = "red", line = 2.5, font = 2)

title(main = "\nthay đổi khoảng cách giữa axis và plot region",
      col.main = "darkgreen")

mark(labels = "par(mgp = c(3, 2, 1)) \nplot(x, y)", x = 2012, y = 8000, cex = 0.6, col_bg = "#faff7f", col = "red")
ani.record()

####

saveGIF(ani.replay(), 
        interval = 2,
        movie.name = 'mgp.gif', ani.width = 600, ani.height = 600)

par(oldpar)

Tài liệu tham khảo

  1. https://jeromyanglim.blogspot.com/2010/05/abbreviations-of-r-commands-explained.html

  2. https://yihui.org/animation/example/animation-package/

  3. https://cran.r-project.org/web/packages/unikn/vignettes/text.html

  4. https://wilkelab.org/gridtext/

  5. https://stackoverflow.com/questions/65420489/how-to-change-text-color-in-a-plot

Sơ kết

Trên đây là mô tả các tham số thường dùng trong Base R Graphics. Để học R bài bản từ A đến Z, thân mời Bạn tham gia khóa học “HDSD R để xử lý dữ liệu” để có nền tảng vững chắc về R nhằm tự tay làm các câu chuyện dữ liệu của riêng mình!

ĐĂNG KÝ NGAY: https://www.tuhocr.com/register