Reputation: 8404
I have the dataframe below and Im trying to create a stacked area chart using ggplot()
and ggplotly()
. But while the chart seems to be produced without errors I cannot display the colored areas.
x_dt<-structure(list(interval_end = c("2021-08-14", "2021-08-14", "2021-08-14",
"2021-08-14", "2021-08-15", "2021-08-15", "2021-08-15", "2021-08-15",
"2021-08-16", "2021-08-16", "2021-08-16", "2021-08-16", "2021-08-17",
"2021-08-17", "2021-08-17", "2021-08-17", "2021-08-18", "2021-08-18",
"2021-08-18", "2021-08-18", "2021-08-19", "2021-08-19", "2021-08-19",
"2021-08-19", "2021-08-20", "2021-08-20", "2021-08-20", "2021-08-20",
"2021-08-21", "2021-08-21", "2021-08-21", "2021-08-21", "2021-08-22",
"2021-08-22", "2021-08-22", "2021-08-22", "2021-08-23", "2021-08-23",
"2021-08-23", "2021-08-23", "2021-08-24", "2021-08-24", "2021-08-24",
"2021-08-24", "2021-08-25", "2021-08-25", "2021-08-25", "2021-08-25",
"2021-08-26", "2021-08-26", "2021-08-26", "2021-08-26", "2021-08-27",
"2021-08-27", "2021-08-27", "2021-08-27", "2021-08-28", "2021-08-28",
"2021-08-28", "2021-08-28", "2021-08-29", "2021-08-29", "2021-08-29",
"2021-08-29", "2021-08-30", "2021-08-30", "2021-08-30", "2021-08-30",
"2021-08-15", "2021-08-15", "2021-08-15", "2021-08-15", "2021-08-16",
"2021-08-16", "2021-08-16", "2021-08-16", "2021-08-17", "2021-08-17",
"2021-08-17", "2021-08-17", "2021-08-18", "2021-08-18", "2021-08-18",
"2021-08-18", "2021-08-19", "2021-08-19", "2021-08-19", "2021-08-19",
"2021-08-20", "2021-08-20", "2021-08-20", "2021-08-20", "2021-08-21",
"2021-08-21", "2021-08-21", "2021-08-21", "2021-08-22", "2021-08-22",
"2021-08-22", "2021-08-22"), inv_bin = c("(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]", "(0.0, 10.0]", "(10.0, 70.0]",
"(70.0, 330.0]", "(330.0, inf]"), N = c(58L, 55L, 38L, 25L, 0L,
9L, 13L, 6L, 2L, 11L, 13L, 5L, 44L, 120L, 69L, 48L, 17L, 103L,
66L, 61L, 20L, 69L, 67L, 63L, 29L, 63L, 69L, 51L, 28L, 41L, 29L,
36L, 2L, 8L, 19L, 10L, 2L, 17L, 16L, 9L, 22L, 65L, 52L, 50L,
34L, 45L, 62L, 68L, 49L, 39L, 54L, 51L, 89L, 70L, 42L, 51L, 59L,
105L, 59L, 65L, 3L, 24L, 23L, 16L, 45L, 36L, 18L, 13L, 58L, 64L,
51L, 31L, 2L, 20L, 26L, 11L, 46L, 131L, 82L, 53L, 61L, 223L,
135L, 109L, 37L, 172L, 133L, 124L, 49L, 132L, 136L, 114L, 57L,
104L, 98L, 87L, 30L, 49L, 48L, 46L)), row.names = c(NA, -100L
), class = c("data.table", "data.frame"))
# Packages
library(ggplot2)
library(dplyr)
library(viridis)
library(hrbrthemes)
library(plotly)
# Plot
p<-ggplot(x_dt, aes(x=interval_end, y=N, fill=inv_bin)) +
geom_area(alpha=0.6 , size=.5, colour="white") +
scale_fill_viridis(discrete = T) +
theme_ipsum() +
ggtitle("Chart 1")
ggplotly(p)
The result is expected to be like:
Upvotes: 0
Views: 50
Reputation: 12699
A bit of data preparation is required:
N
where there are dates and inv_bin with the same value.# Packages
library(ggplot2)
library(dplyr)
library(viridis)
library(hrbrthemes)
library(forcats)
library(plotly)
x_dt1 <-
x_dt %>%
mutate(interval_end = as.Date(interval_end, "%Y-%m-%d"),
inv_bin = factor(inv_bin, levels = c("(0.0, 10.0]", "(10.0, 70.0]", "(70.0, 330.0]", "(330.0, inf]"), ordered = TRUE)) %>%
group_by(interval_end, inv_bin) %>%
summarise(N = sum(N))
p <-
ggplot(x_dt1, aes(x = interval_end, y = N, fill = fct_rev(inv_bin)))+
geom_area(alpha = 0.6 , size = 0.5, colour = "white", stat = "identity", orientation = "x") +
scale_fill_viridis(discrete = TRUE) +
labs(fill = NULL)+
theme_ipsum() +
ggtitle("Chart 1")+
theme(legend.position = "bottom")
p
# not printed
# ggplotly(p)
Created on 2021-09-01 by the reprex package (v2.0.0)
Upvotes: 1