1

I want to plot corresponding pctg in an additive way. stock_exhaustion is level of exhaustion of stock. Exemple, what I have actually :

stock_exhaustion    Type_product   sm    pctg
    (0,10]           C              13.  5.78
    (10,20]          C              20.  8.89 ..

I want to transform it into

stock_exhaustion    Type_product   sm    pctg
    (0,10]           C              13.  5.78
    (10,20]          C              20.  5.78 + 8.89 ..

Is it possible with ggplot ? or should I reshape my table :in this case I have no idea how to indicate to R to sum with the precedent interval

this is mydataset

    res=structure(list(stock_exhaustion = structure(c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 
6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 
10L, 10L), .Label = c("(0,10]", "(10,20]", "(20,30]", "(30,40]", 
"(40,50]", "(50,60]", "(60,70]", "(70,80]", "(80,90]", "(90,100]"
), class = "factor"), Type_product = c("C", "F", "M", 
"S", "C", "F", "M", "S", "C", "F", 
"S", "C", "F", "S", "C", "F", "M", 
"S", "C", "F", "M", "S", "C", "F", 
"M", "S", "C", "F", "M", "S", "C", 
"F", "M", "C", "F", "M"), somme = c(13, 
29, 1, 7, 20, 24, 2, 5, 13, 37, 3, 16, 32, 3, 25, 27, 1, 1, 25, 
22, 2, 1, 33, 14, 3, 1, 29, 19, 4, 1, 33, 9, 9, 18, 9, 25), pctg = c(5.77777777777778, 
13.0630630630631, 2.08333333333333, 38.8888888888889, 8.88888888888889, 
10.8108108108108, 4.16666666666667, 27.7777777777778, 5.77777777777778, 
16.6666666666667, 16.6666666666667, 7.11111111111111, 14.4144144144144, 
16.6666666666667, 11.1111111111111, 12.1621621621622, 2.08333333333333, 
5.55555555555556, 11.1111111111111, 9.90990990990991, 4.16666666666667, 
5.55555555555556, 14.6666666666667, 6.30630630630631, 6.25, 5.55555555555556, 
12.8888888888889, 8.55855855855856, 8.33333333333333, 5.55555555555556, 
14.6666666666667, 4.05405405405405, 18.75, 8, 4.05405405405405, 
52.0833333333333)), .Names = c("stock_exhaustion", "Type_product", 
"somme", "pctg"), row.names = c(NA, -36L), vars = "stock_exhaustion", drop = TRUE, class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
ranell
  • 683
  • 13
  • 29

2 Answers2

3

something like this?

library(tidyverse)

res %>% 
  group_by( Type_product ) %>%
  mutate( pctg_cumsum = cumsum( pctg ) ) %>%
  arrange( Type_product )

# # A tibble: 36 x 5
# # Groups:   Type_product [4]
# stock_exhaustion Type_product somme  pctg pctg_cumsum
#   <fct>            <chr>        <dbl> <dbl>       <dbl>
# 1 (0,10]           C               13  5.78        5.78
# 2 (10,20]          C               20  8.89       14.7 
# 3 (20,30]          C               13  5.78       20.4 
# 4 (30,40]          C               16  7.11       27.6 
# 5 (40,50]          C               25 11.1        38.7 
# 6 (50,60]          C               25 11.1        49.8 
# 7 (60,70]          C               33 14.7        64.4 
# 8 (70,80]          C               29 12.9        77.3 
# 9 (80,90]          C               33 14.7        92.  
# 10 (90,100]         C               18  8         100. 
# # ... with 26 more rows
Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • @denis glad to be on the other side for a change ;-) – Wimpel Sep 12 '18 at 11:23
  • Oh that's perfect ! How did R recognize adding with the last interval ? because intervals arranged as factors ?Thanks a lot ! – ranell Sep 12 '18 at 11:26
  • @ranell happened automatically.. but if you want to be sure, you can always arrange/sort the groups before calculating the cumulative sum. – Wimpel Sep 12 '18 at 11:35
2

You can use cumsum() in aes() of every geom or ggplot at all.

For example with your dataset:

ggplot(data = res,
       aes(x = 1:36,
           y = cumsum(pctg))) + 
  geom_point() +
  geom_line()

enter image description here

Cause ↑this graph makes no sense with your data, maybe it'll more interesting to build with different facets:

  ggplot(data = res,
       aes(x = stock_exhaustion,
           y = cumsum(pctg))) + 
  geom_point() +
  facet_wrap(vars(Type_product), scales = "free_y") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

Also you can read stackoverflow a bit there is a bunch of similar questions

atsyplenkov
  • 1,158
  • 13
  • 25