Hướng Dẫn Vẽ Biểu Đồ Trong R

Cài Đặt Môi Trường


library(ggplot2)
library(dplyr)
data(iris)
data(mtcars)

Biểu Đồ Phân Tán


# Biểu đồ cơ bản
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(title = "Chiều dài đài hoa vs Chiều rộng đài hoa", x = "Đơn vị (cm)", y = "Đơn vị (cm)")

# Phân nhóm màu sắc và đường hồi quy
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 2, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE) +
  theme_minimal()

Biểu Đồ Cột


# Cột đơn giản
ggplot(mtcars, aes(x = factor(cyl))) +
  geom_bar(fill = "steelblue") +
  labs(title = "Số lượng xe theo số xy-lanh", x = "Số xy-lanh", y = "Tần suất")

# Cột nhóm
ggplot(mtcars, aes(x = factor(cyl), fill = factor(am))) +
  geom_bar(position = "dodge") +
  labs(title = "Tỷ lệ hộp số tự động/xe cơ", fill = "Hộp số(0/1)")

Biểu Đồ Hộp


ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
  geom_boxplot() +
  theme_bw() +
  labs(title = "Phân bố chiều dài cánh hoa theo loài")

Biểu Đồ Mật Độ


# Histogram
ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(bins = 20, fill = "orange", alpha = 0.7)

# Mật độ phân nhóm
ggplot(iris, aes(x = Petal.Length, fill = Species)) +
  geom_density(alpha = 0.5)

Biểu Đồ Đường


df <- data.frame(
  time = 1:20,
  value = cumsum(rnorm(20))
)

ggplot(df, aes(x = time, y = value)) +
  geom_line(color = "red", linewidth = 1) +
  geom_point()

Bản Đồ Nhiệt


cor_df <- round(cor(iris[,1:4]), 2)
cor_df <- as.data.frame(as.table(cor_df))

ggplot(cor_df, aes(x = Var1, y = Var2, fill = Freq)) +
  geom_tile() +
  geom_text(aes(label = Freq), color = "white") +
  scale_fill_gradient2(low = "blue", high = "red") +
  theme_minimal()

Biểu Đồ Vĩ Cầm


ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
  geom_violin(alpha = 0.6) +
  geom_jitter(size = 0.5)

Chia Nhóm Biểu Đồ


ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point() +
  facet_wrap(~Species) +
  theme_bw()

Biểu Đồ Thước Đo


iris_sum <- iris %>%
  group_by(Species) %>%
  summarise(
    mean = mean(Petal.Length),
    sd = sd(Petal.Length)
  )

ggplot(iris_sum, aes(x = Species, y = mean, fill = Species)) +
  geom_col() +
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2)

Biểu Đồ Tròn


df <- table(mtcars$cyl) %>% data.frame()
colnames(df) <- c("cyl", "count")

ggplot(df, aes(x = "", y = count, fill = factor(cyl))) +
  geom_col() +
  coord_polar("y") +
  theme_void()

Biểu Đồ Bong Bóng


ggplot(mtcars, aes(x = wt, y = mpg, size = hp, color = factor(cyl))) +
  geom_point(alpha = 0.7) +
  scale_size(range = c(2, 10)) +
  labs(title = "Trọng lượng-Tiêu hao nhiên liệu-Mã lực-Số xy-lanh")

Kiểu Biểu Đồ Chuyên Nghiệp


ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = T) +
  labs(
    title = "Mối quan hệ đặc tính hoa",
    subtitle = "Hồi quy tuyến tính chiều dài đài và cánh",
    x = "Chiều dài đài (cm)",
    y = "Chiều dài cánh (cm)",
    color = "Loài"
  ) +
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5)
  )

Thẻ: R ggplot2 data visualization data analysis statistical graphics

Đăng vào ngày 3 tháng 7 lúc 18:48