0

下午好,

我正在尝试根据以下数据在 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. 每个工作日/周末的交易。

例如:

这种情节,不同的月份有不同的颜色。

我怎样才能开始制作这些地块?我尝试了以下代码:

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

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

4

1 回答 1

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
于 2021-01-09T18:40:58.203 回答