ggplot中每个时间间隔的累积值

时间:2018-09-12 11:12:54

标签: r ggplot2

我想以加法方式绘制对应的pctgstock_exhaustion是库存耗尽的级别。 举例来说,我实际拥有的是:

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

我想将其转换为

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

ggplot是否可能?还是应该重塑表格:在这种情况下,我不知道如何指示R与先验间隔求和

这是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"))

2 个答案:

答案 0 :(得分:3)

像这样吗?

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

答案 1 :(得分:2)

您可以在cumsum()aes()中的geom中使用ggplot

例如您的数据集:

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

enter image description here

原因↑此图对您的数据毫无意义,也许用不同的方面进行构建会更有趣:

  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

此外,您还可以阅读stackoverflow,其中有一堆similar questions