I have the following data table:
dt.data <- structure(list(yyyymm= c("2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07"), value = c(0,
-23760, 720, 720, -13680, 18252, -2160, 3600, 6552, -1420.56,
20534.4, 2160, 5040, 1980, 0, -24552, 744, 744, -4464, 18860.4,
-2232, 3720, 10490.4, -1467.91, 15266.88, 2232, 5208, 2046, 0,
-23760, 720, 720, -720, 18252, -2160, 3600, 10152, -1420.56,
11174.4, 2160, 5040, 1980, 0, -24552, 744, 744, -744, 18860.4,
-2232, 3720), testComp = c("Axpo (CH)", "CEZ (CZ)", "EDF Trading (GB)",
"Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)", "HSE (SI)",
"Mercuria (CH)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (GB)", "SEFE (GB)", "WINGAS (DE)", "Axpo (CH)", "CEZ (CZ)",
"EDF Trading (GB)", "Energie AG (AT)", "Energie Klagenfurt (AT)",
"Engie (FR)", "HSE (SI)", "Mercuria (CH)", "OMV Gas M&T (AT)",
"RAG (AT)", "RWE (DE)", "RWE (GB)", "SEFE (GB)", "WINGAS (DE)",
"Axpo (CH)", "CEZ (CZ)", "EDF Trading (GB)", "Energie AG (AT)",
"Energie Klagenfurt (AT)", "Engie (FR)", "HSE (SI)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)", "RWE (GB)", "SEFE (GB)",
"WINGAS (DE)", "Axpo (CH)", "CEZ (CZ)", "EDF Trading (GB)", "Energie AG (AT)",
"Energie Klagenfurt (AT)", "Engie (FR)", "HSE (SI)", "Mercuria (CH)"
)), row.names = c(NA, -50L), class = c("data.table", "data.frame"
))
I want to create a stacked bar chart, where the y-axis describes the value
of each testComp
and the x-axis describes the column yyyymm
.
How can I do this?
I want to have a legend for testComp
and different colors for each of the testComp
.
Furthermore, it would be nice, when I can customize the hovermode.
Give your dataframe this name
dt.data <- data.table(your datahere)
##check your dataframe with this code
dt.data
##Convert yyyymm column to Date format
dt.data$yyyymm <- as.Date(paste0(dt.data$yyyymm, "-01"))
##Plot stacked bar chart##
p<-ggplot(dt.data, aes(x = yyyymm, y = value, fill = testComp)) +
geom_bar(stat = "identity") +
labs(x = "yyyymm", y = "Value", title = "Stacked Bar Chart") +
scale_fill_manual(values = rainbow(length(unique(dt.data$testComp)))) +
theme_minimal() +
theme(legend.position = "right") +
guides(fill = guide_legend(title = "testComp")) +
scale_x_date(date_labels = "%Y-%m", date_breaks = "1 month") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.key.size = unit(0.7, "cm"))
ggplotly(p, tooltip = c("x", "y", "fill"), hovermode = "x")
The result is this with hovermode on:
Like it, tick it correct, if its correct for you as shown with hovermode in figure