R Markdown

SẢN LƯỢNG 6 THÁNG ĐẦU NĂM

# Thư viện
library(ggplot2)
library(scales)

# Dữ liệu
ure_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6"),
    SanLuong = c(85818, 77528, 86140, 82173, 84911, 80576)
)

# Lũy kế
ure_data$LuyKe <- cumsum(ure_data$SanLuong)

# Hệ số scale để đưa Lũy kế về cùng trục
scale_factor <- max(ure_data$SanLuong) / max(ure_data$LuyKe)

# Biểu đồ
ggplot(ure_data, aes(x = Thang)) +
    
    # Cột sản lượng theo tháng
    geom_col(aes(y = SanLuong), fill = "#4E79A7", alpha = 0.7, width = 0.5) +
    
    # Nhãn trên cột
    geom_text(aes(y = SanLuong, label = format(SanLuong, big.mark = ",")),
              vjust = -0.5, color = "#E15759", size = 3.5, fontface = "bold") +
    
    # Miền lũy kế
    geom_area(aes(y = LuyKe * scale_factor, group = 1),
              fill = "#E15759", alpha = 0.4) +
    
    # Đường và điểm lũy kế
    geom_line(aes(y = LuyKe * scale_factor, group = 1),
              color = "#59A14F", size = 0.8) +
    geom_point(aes(y = LuyKe * scale_factor),
               size = 2, color = "#59A14F") +
    
    # Nhãn lũy kế
    geom_text(aes(y = LuyKe * scale_factor, label = format(LuyKe, big.mark = ",")),
              vjust = 0, color = "black", size = 3.2, fontface = "bold") +
    
    # Trục và nhãn
    labs(
        title = "SẢN LƯỢNG THEO THÁNG VÀ LŨY KẾ",
        x = "Tháng",
        y = "Sản lượng theo tháng (tấn)",
        caption = "Trục phải: sản lượng lũy kế"
    ) +
    
    # Trục phụ cho lũy kế
    scale_y_continuous(
        labels = comma,
        sec.axis = sec_axis(~ . / scale_factor,
                            name = "Lũy kế (tấn)",
                            labels = comma)
    ) +
    
    # Giao diện hiện đại, đậm
    theme_minimal(base_size = 13) +
    theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(color = "black", size = 0.6),
        axis.title.y.left  = element_text(face = "bold", color = "#4E79A7"),
        axis.title.y.right = element_text(face = "bold", color = "#59A14F"),
        axis.title.x = element_text(face = "bold"),
        axis.text = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 14),
        legend.position = "none"
    )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Lũy kế và còn lại

# Dữ liệu kế hoạch và thực hiện
bar_data <- data.frame(
    DoanhNghiep = c("PVN", "PVCFC", "Thực hiện", "Thực hiện"),
    GiaTri = c(910000, 934785, 497146, 934785 - 497146),
    Phan = c("PVN", "PVCFC", "Đã đạt", "Còn lại")
)

# Cập nhật thứ tự cột (PVN → PVCFC → Thực hiện)
bar_data$DoanhNghiep <- factor(bar_data$DoanhNghiep, levels = c("PVN", "PVCFC", "Thực hiện"))

# Thư viện
library(ggplot2)
library(scales)

# Xác định giá trị đỉnh PVCFC
y_ngang <- 934785

# Biểu đồ
ggplot(bar_data, aes(x = DoanhNghiep, y = GiaTri, fill = Phan)) +
    geom_col(width = 0.5, alpha = 0.95, color = "black") +
    
    # Ghi số
    geom_text(aes(label = format(GiaTri, big.mark = ",")),
              position = position_stack(vjust = 0.5),
              color = "white", size = 4, fontface = "bold") +
    
    # Đường gạch ngang từ PVCFC sang Thực hiện
    geom_segment(aes(x = 2, xend = 3, y = y_ngang, yend = y_ngang),
                 color = "black", linetype = "dashed", linewidth = 1) +
    
    # Màu và legend theo đúng thứ tự
    scale_fill_manual(
        values = c(
            "PVN" = "#1f77b4",
            "PVCFC" = "#9467bd",
            "Đã đạt" = "#e15759",
            "Còn lại" = "#bab0ac"
        ),
        breaks = c("PVN", "PVCFC", "Đã đạt", "Còn lại")
    ) +
    
    # Trục
    scale_y_continuous(labels = comma, limits = c(0, 1000000)) +
    labs(
        title = "KẾ HOẠCH PVN - PVCFC VÀ MỤC TIÊU SẮP TỚI",
        x = NULL,
        y = "Sản lượng (tấn)",
        fill = NULL
    ) +
    
    # Giao diện
    theme_minimal(base_size = 13) +
    theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(color = "black"),
        axis.text = element_text(face = "bold"),
        axis.title.y = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 14),
        legend.position = "top",
        legend.text = element_text(face = "bold")
    )

##Biểu đồ tải

library(ggplot2)

data <- data.frame(
    Thang = factor(
        c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6"),
        levels = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6")
    ),
    Tai = c(115.861, 116.111, 116.010, 114.537, 114.658, 112.270)
)

ggplot(data, aes(x = Thang, y = Tai, group = 1)) +
    geom_line(color = "#1f77b4", size = 1.5) +
    geom_point(color = "#d62728", size = 4) +
    geom_text(aes(label = sprintf("%.3f", Tai)), vjust = -1, size = 5) +
    # Đường trung bình 114.9%
    geom_hline(yintercept = 114.9, color = "orange", linetype = "dashed", size = 1) +
    annotate("text", x = 6, y = 115.05, label = "Tải TB 2025: 114.9%", color = "orange", size = 5, hjust = 3.5) +
    # Đường trung bình SVCK 2024 là 114.6%
    geom_hline(yintercept = 114.6, color = "red", linetype = "dashed", size = 1) +
    annotate("text", x = 6, y = 114.75, label = "TB 6 tháng 2024: 114.6%", color = "red", size = 5, hjust = 3.5) +
    labs(
        title = "Biểu đồ tải xưởng Urê từ tháng 1 đến tháng 6",
        x = "Tháng",
        y = "Tải xưởng (%)"
    ) +
    ylim(112, 117) +
    theme_minimal(base_size = 16) +
    theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()
    )

## 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", "Tháng 6"),
    SuDung = c(0.560, 0.562, 0.562, 0.561, 0.565, 0.561)
)

# 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 = "Tiêu hao NH3 theo tháng",
        x = "Tháng",
        y = "Tấn NH3 / 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)
    )

## Tiêu hao CO2

# Thư viện
library(ggplot2)

# Dữ liệu CO₂
co2_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6"),
    SuDung = c(0.725, 0.728, 0.731, 0.734, 0.737, 0.731)
)

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

# Biểu đồ
ggplot(co2_data, aes(x = Thang, y = SuDung)) +
    geom_col(fill = "#59A14F", width = 0.6) +
    
    # Nhãn trên đỉnh cột
    geom_text(aes(label = round(SuDung, 3)), 
              vjust = -0.5, fontface = "bold", size = 4, color = "black") +
    
    # Đường trung bình màu đỏ
    geom_hline(yintercept = tb, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = tb + 0.008,
             label = paste("Trung bình:", round(tb, 5)),
             color = "red", fontface = "bold", size = 4, hjust = 2.5) +
    
    # Đường định mức màu vàng
    geom_hline(yintercept = dinhmuc, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = dinhmuc + 0.003,
             label = "Định mức: 0.750",
             color = "orange", fontface = "bold", size = 4, hjust = 2.7) +
    
    # Cắt trục từ giá trị hợp lý
    coord_cartesian(ylim = c(0.68, 0.755)) +
    
    # Nhãn trục
    labs(
        title = "TIÊU HAO CO2 THEO THÁNG",
        x = "Tháng",
        y = "Tấn CO2 / Tấn Ure"
    ) +
    
    # Giao diện hiện đại + có trục X, Y
    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)
    )

## Tiêu hao điện

# Thư viện
library(ggplot2)

# Dữ liệu
dien_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6"),
    SuDung = c(56.995, 57.396, 56.875, 57.815, 57.764, 57.530)
)

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

# Vẽ biểu đồ
ggplot(dien_data, aes(x = Thang, y = SuDung)) +
    geom_col(fill = "#EDC948", width = 0.6) +
    
    # Nhãn trên đỉnh cột
    geom_text(aes(label = round(SuDung, 3)),
              vjust = -0.5, fontface = "bold", size = 4, color = "black") +
    
    # Đường trung bình màu đỏ
    geom_hline(yintercept = tb, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = tb + 0.81,
             label = paste("Trung bình:", round(tb, 3)),
             color = "red", fontface = "bold", size = 4, hjust = 3.4) +
    
    # Đường định mức màu vàng
    geom_hline(yintercept = dinhmuc, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = dinhmuc + 0.41,
             label = "Định mức: 63.056",
             color = "orange", fontface = "bold", size = 4, hjust = 3.5) +
    
    # Giới hạn trục tung để dễ nhìn
    coord_cartesian(ylim = c(50.5, 64)) +
    
    # Nhãn trục
    labs(
        title = "TIÊU HAO ĐIỆN THEO THÁNG",
        x = "Tháng",
        y = "kWh / 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)
    )

Tiêu hao hơi

# Thư viện
library(ggplot2)

# Dữ liệu
hoi_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5","Tháng 6"),
    SuDung = c(0.9499018, 0.9522588, 0.9538364, 0.9598926, 0.963675578,0.952052831)
)

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

# Biểu đồ
ggplot(hoi_data, aes(x = Thang, y = SuDung)) +
    geom_col(fill = "#76B7B2", width = 0.6) +  # Xanh ngọc hiện đại
    
    # Số liệu trên đỉnh cột
    geom_text(aes(label = round(SuDung, 4)),
              vjust = -0.5, fontface = "bold", size = 4, color = "black") +
    
    # Đường trung bình màu đỏ
    geom_hline(yintercept = tb, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = tb + 0.025,
             label = paste("Trung bình:", round(tb, 4)),
             color = "red", fontface = "bold", size = 4, hjust = 0.5) +
    
    # Đường định mức màu vàng
    geom_hline(yintercept = dinhmuc, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = dinhmuc + 0.009,
             label = "Định mức: 1.011",
             color = "orange", fontface = "bold", size = 4, hjust = 0.5) +
    
    # Giới hạn trục tung để nổi bật khác biệt
    coord_cartesian(ylim = c(0.80, 1.03)) +
    
    # Nhãn biểu đồ
    labs(
        title = "TIÊU HAO HƠI CAO ÁP",
        x = "Tháng",
        y = "Tấn Hơi / Tấn Urea"
    ) +
    
    # Giao diện hiện đại, rõ ràng
    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)
    )

Tiêu hao Năng lượng

# Thư viện
library(ggplot2)

# Dữ liệu
nangluong_data <- data.frame(
    Thang = c("Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6"),
    SuDung = c(3.735, 3.748, 3.751, 3.783, 3.795,3.753 )
)

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

# Biểu đồ
ggplot(nangluong_data, aes(x = Thang, y = SuDung)) +
    geom_col(fill = "#F28E2B", width = 0.6) +  # Màu cam hiện đại
    
    # Nhãn giá trị trên cột
    geom_text(aes(label = round(SuDung, 3)),
              vjust = -0.5, fontface = "bold", size = 4, color = "black") +
    
    # Đường trung bình màu đỏ
    geom_hline(yintercept = tb, color = "red", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = tb + 0.13,
             label = paste("Trung bình:", round(tb, 3)),
             color = "red", fontface = "bold", size = 4, hjust = 2.5) +
    
    # Đường định mức màu vàng
    geom_hline(yintercept = dinhmuc, color = "orange", linetype = "dashed", linewidth = 1) +
    annotate("text", x = 5.2, y = dinhmuc + 0.06,
             label = "Định mức: 4.046",
             color = "orange", fontface = "bold", size = 4, hjust = 2.7) +
    
    # Cắt trục tung để rõ độ chênh
    coord_cartesian(ylim = c(3.10, 4.1)) +
    
    labs(
        title = "TIÊU THỤ NĂNG LƯỢNG",
        x = "Tháng",
        y = "GJ / Tấn Ure"
    ) +
    
    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)
    )

## So sánh năng lương 2025 và 2024

#Dữ liệu gốc
chitieu <- c("T.NH3/UREA", "T.CO2/UREA", "ĐIỆN kWh/T.UREA", "T.HS/UREA", "GJ/T.UREA")
nam2024 <- c(0.563204, 0.733382, 59.6, 0.950546, 3.769464)
nam2025 <- c(0.56183,  0.731,    57.396, 0.9553, 3.761)

# Tính % thay đổi
pt_change <- round((nam2025 - nam2024) / nam2024 * 100, 2)

# Tạo bảng dữ liệu
data <- data.frame(
    ChiTieu = rep(chitieu, 2),
    Nam = rep(c("2024", "2025"), each = 5),
    GiaTri = c(nam2024, nam2025),
    PT = c(rep(NA, 5), pt_change)
)

# Vẽ biểu đồ
ggplot(data, aes(x = ChiTieu, y = GiaTri, fill = Nam)) +
    geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.6) +
    
    # Nhãn giá trị
    geom_text(aes(label = round(GiaTri, 3)),
              position = position_dodge(width = 0.7),
              vjust = -0.5, size = 4, fontface = "bold") +
    
    # Nhãn phần trăm thay đổi (trên cột 2025)
    geom_text(data = dplyr::filter(data, Nam != "2025"),
              aes(label = paste0(ifelse(PT > 0, "+", ""), PT, "%")),
              position = position_dodge(width = 0.7),
              vjust = -3.0, size = 3.8, color = "red", fontface = "italic") +
    
    # Tăng giới hạn trục Y để không che mất nhãn
    coord_cartesian(ylim = c(0, 70)) +
    
    labs(
        title = "SO SÁNH TIÊU HAO TRUNG BÌNH ĐẦU NĂM 2025 VÀ NĂM 2024",
        x = "Chỉ tiêu",
        y = "Giá trị"
    ) +
    
    scale_fill_manual(values = c("2024" = "#F28E2B", "2025" = "#4E79A7")) +
    
    theme_minimal(base_size = 13) +
    theme(
        panel.grid = element_blank(),
        axis.line = element_line(color = "black"),
        axis.text = element_text(face = "bold"),
        axis.title = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 15),
        legend.position = "top",
        legend.title = element_blank()
    )

So sánh sản lượng 2025, 2024

# Thư viện
library(ggplot2)
library(scales)

# Dữ liệu
data <- data.frame(
    Nam = c("2024", "2025"),
    SanLuong = c(499561, 497146)
)

# Tính % thay đổi
pt_change <- round((data$SanLuong[2] - data$SanLuong[1]) / data$SanLuong[1] * 100, 3)

# Biểu đồ
ggplot(data, aes(x = Nam, y = SanLuong, fill = Nam)) +
    geom_col(width = 0.5, color = "black") +
    
    # Nhãn sản lượng trên mỗi cột
    geom_text(aes(label = format(SanLuong, big.mark = ",")), 
              vjust = -0.5, size = 5, fontface = "bold") +
    
    # Nhãn % thay đổi giữa 2 cột
    annotate("text", x = 1.5, 
             y = max(data$SanLuong) + 20, 
             label = paste0("Chênh lệch: ", ifelse(pt_change > 0, "+", ""), pt_change, "%"),
             color = "red", fontface = "bold", size = 5) +
    
    # Cài đặt trục y
    coord_cartesian(ylim = c(415800, 516700)) +
    
    # Nhãn
    labs(
        title = "SO SÁNH SẢN LƯỢNG UREA CÙNG KỲ 2024-2025",
        x = "Năm",
        y = "Sản lượng (tấn)"
    ) +
    
    scale_fill_manual(values = c("2024" = "#F28E2B", "2025" = "#4E79A7")) +
    
    theme_minimal(base_size = 13) +
    theme(
        panel.grid = element_blank(),
        axis.line = element_line(color = "black"),
        axis.text = element_text(face = "bold"),
        axis.title = element_text(face = "bold"),
        plot.title = element_text(face = "bold", size = 15),
        legend.position = "none"
    )

#