I am trying to make a graph that will plot the cumulative sum value of different customers which will reset whenever a new order is placed. When a new order is placed, it will be indicated with a DateTick = 1 and I've tried to add this to my plots with vlines. Unfortunately, the plot will only show me either the correct Vlines or the correct series lines.
The data I'm using looks something like this
> head(CUSTWP)
# A tibble: 6 x 6
# Groups: Customer [1]
Customer YearWeek `Corrected Delta` `Ordered Quantity TU` DateTick ROP
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 CustLoc1 2020-01 46 NA 0 46
2 CustLoc1 2020-02 148 NA 0 194
3 CustLoc1 2020-03 150 NA 0 344
4 CustLoc1 2020-04 186 NA 0 530
5 CustLoc1 2020-05 205 NA 0 735
6 CustLoc1 2020-06 246 NA 0 981
I used below mentioned code to create the graphs.
p <- CUSTWP[CUSTWP$DateTick==1,]
p <- p[,1:2]
vline.dat <- data.frame(z=p$Customer, vl=p$YearWeek)
ggplot(CUSTWP, aes(YearWeek,`ROP`, group=1)) + geom_line(color= 'red', size = 0.8) + geom_vline(aes(xintercept=vl), data=vline.dat, linetype=4) +
facet_grid(Customer ~ ., scales = "free_y") + theme_light() + ggtitle('Reordering Points') +
theme(axis.text.x = element_text(angle = 20, vjust = 1, hjust=0.9), text = element_text(size = 14)) +
scale_x_discrete(guide = guide_axis(check.overlap = TRUE))
When I execute the code, I get a result as can be seen in the link.
The issue with this graph is that the Vlines are the orders DateTicks for all customers rather than the DateTicks grouped by customer. I've tried a different code that somehow produces the correct graphs but also a bunch of incorrect graphs with below-mentioned code.
p <- CUSTWP[CUSTWP$DateTick==1,]
p <- p[,1:2]
vline.dat <- data.frame(z=p$Customer, vl=p$YearWeek)
ggplot(CUSTWP, aes(YearWeek,`ROP`, group=1)) + geom_line(color= 'red', size = 0.8) + geom_vline(aes(xintercept=vl), data=vline.dat, linetype=4) +
facet_grid(Customer ~ z, scales = "free_y") + theme_light() + ggtitle('Reordering Points') +
theme(axis.text.x = element_text(angle = 20, vjust = 1, hjust=0.9), text = element_text(size = 14)) +
scale_x_discrete(guide = guide_axis(check.overlap = TRUE))
The above code creates a matrix of plots but the only correct ones are the plots on the diagonal line running from top left to bottom right.
I would really appreciate your input on this as I've been stuck on this for quite some time. Thank you in advance and apologies for the incorrect posting standards, this is my first post.