0

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. Problematic Graph

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. Matrix of Graphs

Dan Adams
  • 4,971
  • 9
  • 28
Baklava96
  • 1
  • 1
  • The issue is that your dataset `vline.dat` does not have a column `Customer` as you renamed it to `z`. Hence, faceting by `Customer` will have noe effect and you get all vlines in each facet. Why are you not simply using `p` or at least name the column `Customer`. – stefan Jul 28 '22 at 17:00
  • Hi Stefan, thank you so much for your quick reply. I tried using z ~ . instead in the code but then the issue becomes that the vlines are correct and the plotted series lines are incorrect. Only when I combine the two as seen in the last matrix, some graphs will appear (on the diagonal line from top left to bottom right) that correctly combine the vlines and series lines. – Baklava96 Jul 28 '22 at 17:04
  • Using `z` will not work either as in this case `CUSTWP` does not contain a column named `z`. Try with your first code but use `vline.dat <- data.frame(Customer=p$Customer, vl=p$YearWeek)` – stefan Jul 28 '22 at 17:05
  • 1
    You are fantastic Stefan! That actually worked! Can I somehow recommend your answer or upvote? I'm not sure how to do that but want to thank you for your contribution. :-) – Baklava96 Jul 28 '22 at 17:11
  • 1
    Great. Everything fine. Just saying "thank you" is thanks enough. (: Just a reminder for the future: Have a look at how to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and especially the section on how to post data. – stefan Jul 28 '22 at 17:15
  • Thank you! I will take that into account if I have future questions. Hope you have a wonderful evening and thanks again! :-) – Baklava96 Jul 28 '22 at 17:17

0 Answers0