# 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"),
SanLuong = c(85818, 77528, 86140, 82173, 84911)
)
# 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 = 2, color = "black", size = 3.2, fontface = "bold") +
# Trục và nhãn
labs(
title = "BIỂU ĐỒ 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.
##Vẽ biểu đồ so sánh kế hoạch năm pvn pvcfc
# 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, 416570, 934785 - 416570),
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")
)
###