【问题标题】:Cumulative value per interval in ggplotggplot中每个间隔的累积值
【发布时间】:2019-02-17 00:20:29
【问题描述】:

我想以附加的方式绘制对应的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 与先例间隔相加

这是我的数据集

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

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这样的?

    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
    

    【讨论】:

    • @denis 很高兴能站在另一边进行改变;-)
    • 哦,太完美了! R如何识别与最后一个间隔相加?因为间隔安排为因素?非常感谢!
    • @ranell 是自动发生的。但如果你想确定,你可以在计算累积总和之前对组进行排列/排序。
    【解决方案2】:

    您可以在每个geomggplotaes() 中使用cumsum()

    以您的数据集为例:

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

    原因 ↑这张图对你的数据没有意义,也许用不同的方面构建会更有趣:

      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))
    

    你也可以稍微阅读一下stackoverflow,里面有一堆similar questions

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 2012-04-19
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多