Tài liệu này (R Markdown) hướng dẫn sử dụng R Markdown để lưu trữ quá trình phân tích một dữ liệu hay một dự án. Thay vì gõ lệnh trong cửa sổ R (dưới), chúng ta có thể lưu trữ tất cả các hàm trong R Markdown.
R Markdown là công cụ giúp kết hợp mã lệnh R, nội dung văn bản và kết quả phân tích trong cùng một tài liệu. Người dùng có thể chèn mã R để tạo bảng, biểu đồ và các kết quả thống kê, sau đó xuất tài liệu sang nhiều định dạng như HTML, PDF hoặc Word. Nhờ khả năng tự động cập nhật kết quả khi mã nguồn thay đổi, R Markdown hỗ trợ xây dựng các báo cáo có tính nhất quán, minh bạch và dễ tái lập.
Bạn có thể dùng R Markdown để viết hẳn một cuốn sách. Để sử dụng R Markdown một cách hữu hiệu, chúng ta cần phải làm quen với một số cú pháp phổ biến:
Chữ đậm Chữ nghiêng
Strikethrough
Inline code Ví dụ: The mean value is 15.4.
Inline equation: \(y = ax + b\)
Displayed equation:
\[ \bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i \]
library(datasets)
library(NHANES)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(GGally)
library(table1)
##
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
##
## units, units<-
# Dữ liệu NHANES từ package NHANES
data(NHANES)
# Chỉ trích một số biến cần thiết và đưa vào dataframe tên 'df'
df = NHANES[, c("ID", "Gender", "Age", "Race1", "BMI", "Education", "HHIncomeMid")]
# Kiểm tra 6 dòng đầu của df
head(df)
## # A tibble: 6 × 7
## ID Gender Age Race1 BMI Education HHIncomeMid
## <int> <fct> <int> <fct> <dbl> <fct> <int>
## 1 51624 male 34 White 32.2 High School 30000
## 2 51624 male 34 White 32.2 High School 30000
## 3 51624 male 34 White 32.2 High School 30000
## 4 51625 male 4 Other 15.3 <NA> 22500
## 5 51630 female 49 White 30.6 Some College 40000
## 6 51638 male 9 White 16.8 <NA> 87500
# Dùng hàm ggplot trong package ggplot2
ggplot(data=df, aes(x=Age, y=BMI)) + geom_point() + labs(title = "Mối liên quan giữa tuổi và BMI", x = "Tuổi", y = "BMI")
## Warning: Removed 366 rows containing missing values or values outside the scale range
## (`geom_point()`).
# Dùng hàm ggplot trong package ggplot2
ggplot(data=df, aes(x=Age, y=BMI, col=Race1)) + geom_point() + labs(title = "Mối liên quan giữa tuổi và BMI", x = "Tuổi", y = "BMI")
## Warning: Removed 366 rows containing missing values or values outside the scale range
## (`geom_point()`).
# Dùng hàm ggplot trong package ggplot2
ggplot(data=df, aes(x=Age, y=BMI, col=Race1)) + geom_point() + geom_smooth(method="loess") + labs(title = "Mối liên quan giữa tuổi và BMI", x = "Tuổi", y = "BMI")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 366 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 366 rows containing missing values or values outside the scale range
## (`geom_point()`).
# Tính median income by Education
income_by_education <- df %>%
group_by(Education) %>%
summarise(MedianIncome = median(HHIncomeMid, na.rm = TRUE),
.groups = "drop")
# Vẽ biểu đồ
ggplot(income_by_education, aes(x = reorder(Education, MedianIncome), y = MedianIncome, fill=Education, col=Education)) + geom_col() + geom_text(aes(label = scales::comma(MedianIncome)), vjust = -0.1) + labs(title = "Median Household Income by Education", x = "Education", y = "Median household income") + theme(legend.position="none")