在 R Studio 中创建我的交易图

Creating plots of my transactions within R Studio

下午好,

我正在尝试根据以下数据在 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. 每笔交易 Weekday/Weekend.

例如:

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

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

如果我理解你的意思,你想要每个时间段内每个组的观察次数。你可以用 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