The code below constructs a stacked area chart using ggplot. But the chart doesn't look correct. The series "Name2" is negative, but the negative values are not reflected in the other series. On the graph it looks like the cumulative sum of the series is 24, but it is actually 18.
How can I fix this?
library(ggplot2)
d_date <- Sys.Date()
d_value_1 <- c(1,3,4,0,0)
d_value_2 <- c(-1,-2,-8,5,0)
d_value_3 <- c(5,5,4,2,0)
df_plot_1 <- data.frame("date" = seq(d_date, d_date + 4, 1),
"value" = cumsum(d_value_1),
"name" = "Name1")
df_plot_2 <- data.frame("date" = seq(d_date, d_date + 4, 1),
"value" = cumsum(d_value_2),
"name" = "Name2")
df_plot_3 <- data.frame("date" = seq(d_date, d_date + 4, 1),
"value" = cumsum(d_value_3),
"name" = "Name3")
df_plot <- rbind(df_plot_1, df_plot_2, df_plot_3)
ggplot(df_plot, aes(x=date, y=value, fill=name)) +
geom_area()