从三个不同的表格中创建一年中每个季度的堆叠条形图

问题描述 投票:0回答:1

我有三个不同的表格,想要根据一年中每个季度的这三个表格创建一个堆积条形图。每个表代表每个

avgPrice
和每个
quarters
一个
priceDate
值。在我的真实数据中,每个表大约有 19710 行,其中有许多不同的
priceDate

堆积条形图应如下所示:

此处 x 轴代表

quarters
,y 轴代表 €/MWh (
avgPrice
)。

如何绘制/创建堆积条形图?如果我们可以专门查看堆积条形图中的某个

priceDate
,那就太好了。这意味着设计和代码的结构应该使我可以在不同时间点显示/绘制相同的图表(
priceDates
)。

这只是这些表格的子集,其中表格具有以下格式:

dt.HPFC26 <- structure(list(delivDate = structure(c(1L, 91L, 182L, 274L), .Label = c("2026-01-01", 
"2026-01-02", "2026-01-03", "2026-01-04", "2026-01-05", "2026-01-06", 
"2026-01-07", "2026-01-08", "2026-01-09", "2026-01-10", "2026-01-11", 
"2026-01-12", "2026-01-13", "2026-01-14", "2026-01-15", "2026-01-16", 
"2026-01-17", "2026-01-18", "2026-01-19", "2026-01-20", "2026-01-21", 
"2026-01-22", "2026-01-23", "2026-01-24", "2026-01-25", "2026-01-26", 
"2026-01-27", "2026-01-28", "2026-01-29", "2026-01-30", "2026-01-31", 
"2026-02-01", "2026-02-02", "2026-02-03", "2026-02-04", "2026-02-05", 
"2026-02-06", "2026-02-07", "2026-02-08", "2026-02-09", "2026-02-10", 
"2026-02-11", "2026-02-12", "2026-02-13", "2026-02-14", "2026-02-15", 
"2026-02-16", "2026-02-17", "2026-02-18", "2026-02-19", "2026-02-20", 
"2026-02-21", "2026-02-22", "2026-02-23", "2026-02-24", "2026-02-25", 
"2026-02-26", "2026-02-27", "2026-02-28", "2026-03-01", "2026-03-02", 
"2026-03-03", "2026-03-04", "2026-03-05", "2026-03-06", "2026-03-07", 
"2026-03-08", "2026-03-09", "2026-03-10", "2026-03-11", "2026-03-12", 
"2026-03-13", "2026-03-14", "2026-03-15", "2026-03-16", "2026-03-17", 
"2026-03-18", "2026-03-19", "2026-03-20", "2026-03-21", "2026-03-22", 
"2026-03-23", "2026-03-24", "2026-03-25", "2026-03-26", "2026-03-27", 
"2026-03-28", "2026-03-29", "2026-03-30", "2026-03-31", "2026-04-01", 
"2026-04-02", "2026-04-03", "2026-04-04", "2026-04-05", "2026-04-06", 
"2026-04-07", "2026-04-08", "2026-04-09", "2026-04-10", "2026-04-11", 
"2026-04-12", "2026-04-13", "2026-04-14", "2026-04-15", "2026-04-16", 
"2026-04-17", "2026-04-18", "2026-04-19", "2026-04-20", "2026-04-21", 
"2026-04-22", "2026-04-23", "2026-04-24", "2026-04-25", "2026-04-26", 
"2026-04-27", "2026-04-28", "2026-04-29", "2026-04-30", "2026-05-01", 
"2026-05-02", "2026-05-03", "2026-05-04", "2026-05-05", "2026-05-06", 
"2026-05-07", "2026-05-08", "2026-05-09", "2026-05-10", "2026-05-11", 
"2026-05-12", "2026-05-13", "2026-05-14", "2026-05-15", "2026-05-16", 
"2026-05-17", "2026-05-18", "2026-05-19", "2026-05-20", "2026-05-21", 
"2026-05-22", "2026-05-23", "2026-05-24", "2026-05-25", "2026-05-26", 
"2026-05-27", "2026-05-28", "2026-05-29", "2026-05-30", "2026-05-31", 
"2026-06-01", "2026-06-02", "2026-06-03", "2026-06-04", "2026-06-05", 
"2026-06-06", "2026-06-07", "2026-06-08", "2026-06-09", "2026-06-10", 
"2026-06-11", "2026-06-12", "2026-06-13", "2026-06-14", "2026-06-15", 
"2026-06-16", "2026-06-17", "2026-06-18", "2026-06-19", "2026-06-20", 
"2026-06-21", "2026-06-22", "2026-06-23", "2026-06-24", "2026-06-25", 
"2026-06-26", "2026-06-27", "2026-06-28", "2026-06-29", "2026-06-30", 
"2026-07-01", "2026-07-02", "2026-07-03", "2026-07-04", "2026-07-05", 
"2026-07-06", "2026-07-07", "2026-07-08", "2026-07-09", "2026-07-10", 
"2026-07-11", "2026-07-12", "2026-07-13", "2026-07-14", "2026-07-15", 
"2026-07-16", "2026-07-17", "2026-07-18", "2026-07-19", "2026-07-20", 
"2026-07-21", "2026-07-22", "2026-07-23", "2026-07-24", "2026-07-25", 
"2026-07-26", "2026-07-27", "2026-07-28", "2026-07-29", "2026-07-30", 
"2026-07-31", "2026-08-01", "2026-08-02", "2026-08-03", "2026-08-04", 
"2026-08-05", "2026-08-06", "2026-08-07", "2026-08-08", "2026-08-09", 
"2026-08-10", "2026-08-11", "2026-08-12", "2026-08-13", "2026-08-14", 
"2026-08-15", "2026-08-16", "2026-08-17", "2026-08-18", "2026-08-19", 
"2026-08-20", "2026-08-21", "2026-08-22", "2026-08-23", "2026-08-24", 
"2026-08-25", "2026-08-26", "2026-08-27", "2026-08-28", "2026-08-29", 
"2026-08-30", "2026-08-31", "2026-09-01", "2026-09-02", "2026-09-03", 
"2026-09-04", "2026-09-05", "2026-09-06", "2026-09-07", "2026-09-08", 
"2026-09-09", "2026-09-10", "2026-09-11", "2026-09-12", "2026-09-13", 
"2026-09-14", "2026-09-15", "2026-09-16", "2026-09-17", "2026-09-18", 
"2026-09-19", "2026-09-20", "2026-09-21", "2026-09-22", "2026-09-23", 
"2026-09-24", "2026-09-25", "2026-09-26", "2026-09-27", "2026-09-28", 
"2026-09-29", "2026-09-30", "2026-10-01", "2026-10-02", "2026-10-03", 
"2026-10-04", "2026-10-05", "2026-10-06", "2026-10-07", "2026-10-08", 
"2026-10-09", "2026-10-10", "2026-10-11", "2026-10-12", "2026-10-13", 
"2026-10-14", "2026-10-15", "2026-10-16", "2026-10-17", "2026-10-18", 
"2026-10-19", "2026-10-20", "2026-10-21", "2026-10-22", "2026-10-23", 
"2026-10-24", "2026-10-25", "2026-10-26", "2026-10-27", "2026-10-28", 
"2026-10-29", "2026-10-30", "2026-10-31", "2026-11-01", "2026-11-02", 
"2026-11-03", "2026-11-04", "2026-11-05", "2026-11-06", "2026-11-07", 
"2026-11-08", "2026-11-09", "2026-11-10", "2026-11-11", "2026-11-12", 
"2026-11-13", "2026-11-14", "2026-11-15", "2026-11-16", "2026-11-17", 
"2026-11-18", "2026-11-19", "2026-11-20", "2026-11-21", "2026-11-22", 
"2026-11-23", "2026-11-24", "2026-11-25", "2026-11-26", "2026-11-27", 
"2026-11-28", "2026-11-29", "2026-11-30", "2026-12-01", "2026-12-02", 
"2026-12-03", "2026-12-04", "2026-12-05", "2026-12-06", "2026-12-07", 
"2026-12-08", "2026-12-09", "2026-12-10", "2026-12-11", "2026-12-12", 
"2026-12-13", "2026-12-14", "2026-12-15", "2026-12-16", "2026-12-17", 
"2026-12-18", "2026-12-19", "2026-12-20", "2026-12-21", "2026-12-22", 
"2026-12-23", "2026-12-24", "2026-12-25", "2026-12-26", "2026-12-27", 
"2026-12-28", "2026-12-29", "2026-12-30", "2026-12-31"), class = "factor"), 
    priceDate = structure(c(29L, 25L, 29L, 5L), .Label = c("2024-01-02", 
    "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-08", "2024-01-09", 
    "2024-01-10", "2024-01-11", "2024-01-12", "2024-01-15", "2024-01-16", 
    "2024-01-17", "2024-01-18", "2024-01-19", "2024-01-22", "2024-01-23", 
    "2024-01-24", "2024-01-25", "2024-01-26", "2024-01-29", "2024-01-30", 
    "2024-01-31", "2024-02-01", "2024-02-02", "2024-02-05", "2024-02-06", 
    "2024-02-07", "2024-02-08", "2024-02-09", "2024-02-12", "2024-02-13", 
    "2024-02-14", "2024-02-15", "2024-02-16", "2024-02-19", "2024-02-20", 
    "2024-02-21", "2024-02-22", "2024-02-23", "2024-02-26", "2024-02-27", 
    "2024-02-28", "2024-02-29", "2024-03-01", "2024-03-04", "2024-03-05", 
    "2024-03-06", "2024-03-07", "2024-03-08", "2024-03-11", "2024-03-12", 
    "2024-03-13", "2024-03-14", "2024-03-15"), class = "factor"), 
    priceIndex = c("HPFC DE", "HPFC DE", "HPFC DE", 
    "HPFC DE"), avgPrice = c(61.55, 75, 69.12, 94.4), quarters = c("Q1", 
    "Q2", "Q3", "Q4")), row.names = c(NA, -4L), class = c("data.table", 
"data.frame"))

dt.HPFC27 <- structure(list(delivDate = structure(c(1L, 91L, 182L, 274L), .Label = c("2027-01-01", 
"2027-01-02", "2027-01-03", "2027-01-04", "2027-01-05", "2027-01-06", 
"2027-01-07", "2027-01-08", "2027-01-09", "2027-01-10", "2027-01-11", 
"2027-01-12", "2027-01-13", "2027-01-14", "2027-01-15", "2027-01-16", 
"2027-01-17", "2027-01-18", "2027-01-19", "2027-01-20", "2027-01-21", 
"2027-01-22", "2027-01-23", "2027-01-24", "2027-01-25", "2027-01-26", 
"2027-01-27", "2027-01-28", "2027-01-29", "2027-01-30", "2027-01-31", 
"2027-02-01", "2027-02-02", "2027-02-03", "2027-02-04", "2027-02-05", 
"2027-02-06", "2027-02-07", "2027-02-08", "2027-02-09", "2027-02-10", 
"2027-02-11", "2027-02-12", "2027-02-13", "2027-02-14", "2027-02-15", 
"2027-02-16", "2027-02-17", "2027-02-18", "2027-02-19", "2027-02-20", 
"2027-02-21", "2027-02-22", "2027-02-23", "2027-02-24", "2027-02-25", 
"2027-02-26", "2027-02-27", "2027-02-28", "2027-03-01", "2027-03-02", 
"2027-03-03", "2027-03-04", "2027-03-05", "2027-03-06", "2027-03-07", 
"2027-03-08", "2027-03-09", "2027-03-10", "2027-03-11", "2027-03-12", 
"2027-03-13", "2027-03-14", "2027-03-15", "2027-03-16", "2027-03-17", 
"2027-03-18", "2027-03-19", "2027-03-20", "2027-03-21", "2027-03-22", 
"2027-03-23", "2027-03-24", "2027-03-25", "2027-03-26", "2027-03-27", 
"2027-03-28", "2027-03-29", "2027-03-30", "2027-03-31", "2027-04-01", 
"2027-04-02", "2027-04-03", "2027-04-04", "2027-04-05", "2027-04-06", 
"2027-04-07", "2027-04-08", "2027-04-09", "2027-04-10", "2027-04-11", 
"2027-04-12", "2027-04-13", "2027-04-14", "2027-04-15", "2027-04-16", 
"2027-04-17", "2027-04-18", "2027-04-19", "2027-04-20", "2027-04-21", 
"2027-04-22", "2027-04-23", "2027-04-24", "2027-04-25", "2027-04-26", 
"2027-04-27", "2027-04-28", "2027-04-29", "2027-04-30", "2027-05-01", 
"2027-05-02", "2027-05-03", "2027-05-04", "2027-05-05", "2027-05-06", 
"2027-05-07", "2027-05-08", "2027-05-09", "2027-05-10", "2027-05-11", 
"2027-05-12", "2027-05-13", "2027-05-14", "2027-05-15", "2027-05-16", 
"2027-05-17", "2027-05-18", "2027-05-19", "2027-05-20", "2027-05-21", 
"2027-05-22", "2027-05-23", "2027-05-24", "2027-05-25", "2027-05-26", 
"2027-05-27", "2027-05-28", "2027-05-29", "2027-05-30", "2027-05-31", 
"2027-06-01", "2027-06-02", "2027-06-03", "2027-06-04", "2027-06-05", 
"2027-06-06", "2027-06-07", "2027-06-08", "2027-06-09", "2027-06-10", 
"2027-06-11", "2027-06-12", "2027-06-13", "2027-06-14", "2027-06-15", 
"2027-06-16", "2027-06-17", "2027-06-18", "2027-06-19", "2027-06-20", 
"2027-06-21", "2027-06-22", "2027-06-23", "2027-06-24", "2027-06-25", 
"2027-06-26", "2027-06-27", "2027-06-28", "2027-06-29", "2027-06-30", 
"2027-07-01", "2027-07-02", "2027-07-03", "2027-07-04", "2027-07-05", 
"2027-07-06", "2027-07-07", "2027-07-08", "2027-07-09", "2027-07-10", 
"2027-07-11", "2027-07-12", "2027-07-13", "2027-07-14", "2027-07-15", 
"2027-07-16", "2027-07-17", "2027-07-18", "2027-07-19", "2027-07-20", 
"2027-07-21", "2027-07-22", "2027-07-23", "2027-07-24", "2027-07-25", 
"2027-07-26", "2027-07-27", "2027-07-28", "2027-07-29", "2027-07-30", 
"2027-07-31", "2027-08-01", "2027-08-02", "2027-08-03", "2027-08-04", 
"2027-08-05", "2027-08-06", "2027-08-07", "2027-08-08", "2027-08-09", 
"2027-08-10", "2027-08-11", "2027-08-12", "2027-08-13", "2027-08-14", 
"2027-08-15", "2027-08-16", "2027-08-17", "2027-08-18", "2027-08-19", 
"2027-08-20", "2027-08-21", "2027-08-22", "2027-08-23", "2027-08-24", 
"2027-08-25", "2027-08-26", "2027-08-27", "2027-08-28", "2027-08-29", 
"2027-08-30", "2027-08-31", "2027-09-01", "2027-09-02", "2027-09-03", 
"2027-09-04", "2027-09-05", "2027-09-06", "2027-09-07", "2027-09-08", 
"2027-09-09", "2027-09-10", "2027-09-11", "2027-09-12", "2027-09-13", 
"2027-09-14", "2027-09-15", "2027-09-16", "2027-09-17", "2027-09-18", 
"2027-09-19", "2027-09-20", "2027-09-21", "2027-09-22", "2027-09-23", 
"2027-09-24", "2027-09-25", "2027-09-26", "2027-09-27", "2027-09-28", 
"2027-09-29", "2027-09-30", "2027-10-01", "2027-10-02", "2027-10-03", 
"2027-10-04", "2027-10-05", "2027-10-06", "2027-10-07", "2027-10-08", 
"2027-10-09", "2027-10-10", "2027-10-11", "2027-10-12", "2027-10-13", 
"2027-10-14", "2027-10-15", "2027-10-16", "2027-10-17", "2027-10-18", 
"2027-10-19", "2027-10-20", "2027-10-21", "2027-10-22", "2027-10-23", 
"2027-10-24", "2027-10-25", "2027-10-26", "2027-10-27", "2027-10-28", 
"2027-10-29", "2027-10-30", "2027-10-31", "2027-11-01", "2027-11-02", 
"2027-11-03", "2027-11-04", "2027-11-05", "2027-11-06", "2027-11-07", 
"2027-11-08", "2027-11-09", "2027-11-10", "2027-11-11", "2027-11-12", 
"2027-11-13", "2027-11-14", "2027-11-15", "2027-11-16", "2027-11-17", 
"2027-11-18", "2027-11-19", "2027-11-20", "2027-11-21", "2027-11-22", 
"2027-11-23", "2027-11-24", "2027-11-25", "2027-11-26", "2027-11-27", 
"2027-11-28", "2027-11-29", "2027-11-30", "2027-12-01", "2027-12-02", 
"2027-12-03", "2027-12-04", "2027-12-05", "2027-12-06", "2027-12-07", 
"2027-12-08", "2027-12-09", "2027-12-10", "2027-12-11", "2027-12-12", 
"2027-12-13", "2027-12-14", "2027-12-15", "2027-12-16", "2027-12-17", 
"2027-12-18", "2027-12-19", "2027-12-20", "2027-12-21", "2027-12-22", 
"2027-12-23", "2027-12-24", "2027-12-25", "2027-12-26", "2027-12-27", 
"2027-12-28", "2027-12-29", "2027-12-30", "2027-12-31"), class = "factor"), 
    priceDate = structure(c(44L, 28L, 28L, 25L), .Label = c("2024-01-02", 
    "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-08", "2024-01-09", 
    "2024-01-10", "2024-01-11", "2024-01-12", "2024-01-15", "2024-01-16", 
    "2024-01-17", "2024-01-18", "2024-01-19", "2024-01-22", "2024-01-23", 
    "2024-01-24", "2024-01-25", "2024-01-26", "2024-01-29", "2024-01-30", 
    "2024-01-31", "2024-02-01", "2024-02-02", "2024-02-05", "2024-02-06", 
    "2024-02-07", "2024-02-08", "2024-02-09", "2024-02-12", "2024-02-13", 
    "2024-02-14", "2024-02-15", "2024-02-16", "2024-02-19", "2024-02-20", 
    "2024-02-21", "2024-02-22", "2024-02-23", "2024-02-26", "2024-02-27", 
    "2024-02-28", "2024-02-29", "2024-03-01", "2024-03-04", "2024-03-05", 
    "2024-03-06", "2024-03-07", "2024-03-08", "2024-03-11", "2024-03-12", 
    "2024-03-13", "2024-03-14", "2024-03-15"), class = "factor"), 
    priceIndex = c("KELAG HPFC DE", "KELAG HPFC DE", "KELAG HPFC DE", 
    "KELAG HPFC DE"), avgPrice = c(55.17, 70.52, 66.86, 70.26
    ), quarters = c("Q1", "Q2", "Q3", "Q4")), row.names = c(NA, 
-4L), class = c("data.table", "data.frame"))

dt.eex26 <- structure(list(delivDate = structure(c(20454, 20544, 20635, 20727
), class = "Date"), priceDate = structure(c(19724, 19730, 19725, 
19746), class = "Date"), priceIndex = c("EEX DEBQ", "EEX DEBQ", 
"EEX DEBQ", "EEX DEBQ"), avgPrice = c(99.07, 75.32, 83.16, 79.02
), quarters = c("Q1", "Q2", "Q3", "Q4")), row.names = c(NA, -4L
), class = c("data.table", "data.frame"))
r plotly bar-chart rstudio stacked-bar-chart
1个回答
0
投票

您所包含的图像显示的是躲避,而不是堆叠。在任何一种情况下,您的数据框都包含相同的列,因此可以使用

rbind
将它们合并到一个大表中,并且
priceIndex
列用于形成颜色/图例的基础。

其他一切都只是造型:

library(tidyverse)

rbind(dt.eex26, dt.HPFC26, dt.HPFC27) %>%
  mutate(index = factor(case_when(
                 priceIndex == "EEX DEBQ" ~ "Settlement EEX",
                 priceIndex == "HPFC DE" ~ "HPFC (2026)",
                 priceIndex == "KELAG HPFC DE" ~ "HPFC (2027)"),
                 levels = c("HPFC (2026)", "Settlement EEX", "HPFC (2027)")
  )) %>%
  ggplot(aes(quarters, avgPrice, fill = index)) +
  geom_col(width = 0.2, position = position_dodge(0.8)) +
  geom_vline(xintercept = c(1.5, 2.5, 3.5), linetype = 2, color = '#5377cb') +
  scale_fill_manual(NULL, 
                    values = c("#04aa6c", "#f8a1c7", "#c7942e")) +
  scale_y_continuous('Euros / MWh', expand = c(0, 0)) +
  theme_classic(base_size = 16) +
  theme(axis.line = element_line(color = "#5377cb"),
        text = element_text(color = "#5377cb"),
        axis.text = element_text(color = "#5377cb"),
        axis.ticks = element_line(color = "#5377cb"),
        legend.text = element_text(color = c("gray30")))

© www.soinside.com 2019 - 2024. All rights reserved.