【问题标题】:Creating plots of my transactions within R Studio在 R Studio 中创建我的交易图
【发布时间】:2021-01-09 18:15:53
【问题描述】:

下午好,

我正在尝试根据以下数据在 R Studio 中创建一些图:

structure(list(Transaction = c(1L, 2L, 2L, 3L, 3L, 3L), Item = c("Bread", 
"Scandinavian", "Scandinavian", "Hot chocolate", "Jam", "Cookies"
), period_day = c("morning", "morning", "morning", "morning", 
"morning", "morning"), weekday_weekend = c("weekend", "weekend", 
"weekend", "weekend", "weekend", "weekend"), Month = c("October", 
"October", "October", "October", "October", "October"), Time = c("09", 
"10", "10", "10", "10", "10")), row.names = c(NA, 6L), class = "data.frame")

我正在尝试创建条形图以直观地探索数据,例如,我想绘制:

  1. 每月交易。
  2. 一天中每个时间的交易(早上、下午、晚上和晚上)。
  3. 一天中每小时的交易次数(01、02、03 等)。
  4. 每个工作日/周末的交易。

例如:

This kind of plot, with different colours for the different months.

如何开始制作这些地块?我试过以下代码:

SalesByTime <- bb_raw %>%
  group_by(Time, Item) %>%
  summarise(Transactions = sum(Item))

但我相信这是交易数量的总和,而不是销售频率(见下文)。

structure(list(Time = c("01", "07", "08", "09", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
), Transactions = c(4090L, 59889L, 3143112L, 9168556L, 12679593L, 
15159832L, 14129139L, 13231165L, 13633823L, 11081386L, 7053231L, 
1969450L, 289382L, 223385L, 104967L, 16411L, 76746L, 22825L)), row.names = c(NA, 
-18L), class = c("tbl_df", "tbl", "data.frame"))

有什么建议吗?任何帮助将不胜感激。如果我能提供更多信息,请告诉我。

【问题讨论】:

    标签: r gplots exploratory


    【解决方案1】:

    如果我理解您的意思,您需要每个时间段内每组的观察次数。您可以使用dplyr::n() 来执行此操作。

    df %>%
      group_by(Time, Item) %>%
      summarise(Transactions = n())
    
    #   Time  Item          Transactions
    #   <chr> <chr>                <int>
    # 1 09    Bread                    1
    # 2 10    Cookies                  1
    # 3 10    Hot chocolate            1
    # 4 10    Jam                      1
    # 5 10    Scandinavian             2
    

    要按时间段分组,请按您想要的时间段分组。

    df %>%
      group_by(Time) %>%
      summarise(Transactions = n())
    
    #   Time  Transactions
    #   <chr>        <int>
    # 1 09               1
    # 2 10               5
    

    【讨论】:

    • 基本上,是的!但是,我只对交易数量感兴趣,而不是具体的项目名称。因此,如果在上午 10 点到 11 点之间售出 3 杯咖啡、2 块热巧克力和 1 块果酱,我希望只需要时间 - 10,交易 - 6。
    猜你喜欢
    • 2015-08-02
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多