rgplotsexploratory

Creating plots of my transactions within R Studio


Good afternoon,

I am trying to create some plots within R Studio from the following data:

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

I am trying to create bar charts to explore the data visually, for instance, I would like to plot:

  1. Transactions per Month.
  2. Transactions per Time of Day (Morning, Afternoon, Evening and Night).
  3. Transactions per Hour of Day (01, 02, 03, etc.).
  4. Transactions per Weekday/Weekend.

For example:

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

How can I start to produce these plots? I have tried the following code:

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

But I believe this was taking the sum of the transaction numbers, not the frequency of the sales (see below).

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

Any suggestions? Any help would be massively appreciated. Please let me know if there's any more information I can provide.


Solution

  • If I'm understanding you, you want the number of observations within each group per time period. You can do this with 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
    

    To group by just time period, group by the time period(s) you want.

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