R Markdown

Vẽ biểu đồ tg vsth

# Thư viện
library(ggplot2)

# Dữ liệu 2025
data <- data.frame(
    Ngay = as.Date(c("2025-01-23", "2025-02-26", "2025-04-06", "2025-05-03", "2025-05-26")),
    SoNgay = c(27, 34, 39, 27, 23)
)

# Trung bình
tb_2025 <- round(mean(data$SoNgay), 2)   # TB: 30
tb_2024 <- 25.03                         # Đã biết từ trước

# Vẽ biểu đồ
ggplot(data, aes(x = Ngay, y = SoNgay)) +
    geom_col(fill = "#1f77b4", width = 15) +
    
    # Ghi số ngày trên cột
    geom_text(aes(label = SoNgay), vjust = -0.5, fontface = "bold", size = 4) +
    
    # Đường TB 2025
    geom_hline(yintercept = tb_2025, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = min(data$Ngay), y = tb_2025 + 0.9,
             label = paste("TB 2025:", tb_2025, "Ngày"),
             color = "red", fontface = "bold", hjust = -2, size = 4) +
    
    # Đường TB 2024
    geom_hline(yintercept = tb_2024, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = min(data$Ngay), y = tb_2024 + 0.9,
             label = paste("TB đầu năm 2024:", tb_2024, "Ngày"),
             color = "orange", fontface = "bold", hjust = -1, size = 4) +
    
    # Trục ngày
    scale_x_date(date_labels = "%d/%m", breaks = data$Ngay) +
    
    labs(
        title = "CHU KỲ CHẠY CỤM TẠO HẠT",
        x = "Ngày",
        y = "Số ngày vận hành"
    ) +
    
    theme_minimal(base_size = 13) +
    theme(
        panel.grid = element_blank(),
        axis.text = element_text(face = "bold"),
        axis.title = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 14)
    )

## biểu đồ tiêu hao NH3

# Thư viện
library(ggplot2)

# Dữ liệu
nh3_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5"),
    SuDung = c(0.560, 0.562, 0.562, 0.561, 0.565)
)

# Trung bình & định mức
tb <- mean(nh3_data$SuDung)
dinhmuc <- 0.568

# Biểu đồ
ggplot(nh3_data, aes(x = Thang, y = SuDung)) +
    geom_col(fill = "#4E79A7", width = 0.6) +
    
    # Nhãn giá trị trên đỉnh cột (Legend nội tại)
    geom_text(aes(label = round(SuDung, 3)), 
              vjust = -0.5, fontface = "bold", size = 4, color = "black") +
    
    # Đường trung bình
    geom_hline(yintercept = tb, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = tb + 0.0014,
             label = paste("Trung bình:", round(tb, 5)),
             color = "red", fontface = "bold", size = 4, hjust = 1.9) +
    
    # Đường định mức
    geom_hline(yintercept = dinhmuc, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = dinhmuc + 0.0014,
             label = "Định mức: 0.568",
             color = "orange", fontface = "bold", size = 4, hjust = 2) +
    
    # Trục tung từ 0.54
    coord_cartesian(ylim = c(0.54, NA)) +
    
    # Nhãn biểu đồ
    labs(
        title = "Suất tiêu thụ NH₃ theo tháng",
        x = "Tháng",
        y = "Tấn NH₃ / Tấn Ure"
    ) +
    
    # Giao diện
    theme_minimal(base_size = 13) +
    theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.x = element_line(color = "black", linewidth = 0.8),
        axis.line.y = element_line(color = "black", linewidth = 0.8),
        axis.text = element_text(face = "bold"),
        axis.title = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 14)
    )

## lưu ý hàm # Giới hạn hiển thị trục Y (không loại dữ liệu) coord_cartesian(ylim = c(0.54, NA)) +

labs(
    title = "Suất tiêu thụ NH₃ theo tháng",
    x = "Tháng",
    y = "Tấn NH3/Tấn Ure"