library(gridExtra)
library(grid)
library(ggtext)
set.seed(2023)
dashboard_data <- list(
kpi = tibble(
metric = c("総売上", "新規顧客", "リピート率", "顧客満足度"),
current = c(12500000, 1250, 68.5, 4.2),
target = c(12000000, 1200, 70.0, 4.5),
unit = c("¥", "人", "%", "点")
),
timeseries = tibble(
date = seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "week"),
sales = cumsum(rnorm(53, 240000, 50000)),
orders = round(rnorm(53, 120, 30)),
customers = round(rnorm(53, 80, 20))
),
regional = tibble(
region = c("北海道", "東北", "関東", "中部", "関西", "中国", "四国", "九州"),
sales = c(1200, 980, 4500, 2100, 3200, 850, 420, 1100),
growth = c(5.2, -2.1, 8.7, 3.4, 6.8, -0.5, 12.3, 4.1)
),
category = tibble(
category = c("エレクトロニクス", "ファッション", "ホーム&ガーデン", "スポーツ", "書籍"),
sales = c(4200, 3100, 2800, 1900, 1500),
profit_margin = c(18.5, 45.2, 32.1, 25.8, 12.3)
)
)
create_kpi_card <- function(metric, current, target, unit) {
achievement <- current / target
color <- if (achievement >= 1) brand_colors["success"] else brand_colors["warning"]
ggplot() +
geom_rect(aes(xmin = 0, xmax = 1, ymin = 0, ymax = 1),
fill = "white", color = color, size = 2) +
annotate("text", x = 0.5, y = 0.8,
label = metric, size = 5, fontface = "bold",
color = brand_colors["text"]) +
annotate("text", x = 0.5, y = 0.5,
label = paste0(format(current, big.mark = ","), unit),
size = 8, fontface = "bold", color = color) +
annotate("text", x = 0.5, y = 0.2,
label = paste0("目標: ", format(target, big.mark = ","), unit,
" (", round((achievement - 1) * 100, 1), "%)\"),
size = 3, color = brand_colors["neutral"]) +
xlim(0, 1) + ylim(0, 1) +
theme_void() +
theme(
plot.background = element_rect(fill = brand_colors["background"], color = NA),
plot.margin = margin(10, 10, 10, 10)
)
}
kpi_cards <- map2(
dashboard_data$kpi$metric,
pmap(list(dashboard_data$kpi$current, dashboard_data$kpi$target, dashboard_data$kpi$unit), c),
~ create_kpi_card(.x, .y[[1]], .y[[2]], .y[[3]])
)
main_timeseries <- dashboard_data$timeseries %>%
ggplot(aes(x = date, y = sales)) +
geom_area(fill = brand_colors["secondary"], alpha = 0.3) +
geom_line(color = brand_colors["primary"], size = 1.2) +
geom_point(color = brand_colors["primary"], size = 0.8) +
scale_x_date(date_labels = "%m月", date_breaks = "2 months") +
scale_y_continuous(
labels = label_currency(prefix = "¥", suffix = "M", scale = 1e-6)
) +
labs(
title = "累積売上推移",
x = NULL,
y = "売上高"
) +
theme_corporate(base_size = 10)
regional_chart <- dashboard_data$regional %>%
arrange(desc(sales)) %>%
mutate(region = factor(region, levels = region)) %>%
ggplot(aes(x = region, y = sales, fill = growth > 0)) +
geom_col(width = 0.7) +
geom_text(aes(label = paste0(growth, "%")),
vjust = -0.5, size = 3, fontface = "bold") +
scale_fill_manual(values = c("FALSE" = brand_colors["warning"],
"TRUE" = brand_colors["success"])) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(
title = "地域別売上と成長率",
x = NULL,
y = "売上(百万円)"
) +
theme_corporate(base_size = 10) +
theme(
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)
)
category_chart <- dashboard_data$category %>%
ggplot(aes(x = sales, y = profit_margin, size = sales, fill = category)) +
geom_point(alpha = 0.8, shape = 21, color = "white", stroke = 1) +
geom_text(aes(label = category), size = 3,
color = brand_colors["text"], fontface = "bold",
nudge_y = 2) +
scale_fill_corporate() +
scale_size_continuous(range = c(5, 15), guide = "none") +
labs(
title = "カテゴリ別売上vs利益率",
x = "売上(百万円)",
y = "利益率(%)"
) +
theme_corporate(base_size = 10) +
theme(legend.position = "none")
print("ダッシュボードコンポーネントが作成されました")