# 1. Import data ----------------------------------------------------------



library(readxl)
Sales <- read_xlsx("Line_Graphs.xlsx", sheet = "Sales")
Sales
## # A tibble: 13 x 3
##    Period Product_A_Sold Product_B_Sold
##     <dbl>          <dbl>          <dbl>
##  1      1             50             62
##  2      2             53             66
##  3      3             57             72
##  4      4             59             75
##  5      5             65             79
##  6      6             67             83
##  7      7             72             88
##  8      8             76             92
##  9      9             79             96
## 10     10             83             98
## 11     11             86            102
## 12     12             93            106
## 13     13             96            109
colnames(Sales)[2] <- "Product_A"
colnames(Sales)[3] <- "Product_B"


# 2. Melt data for plotting -----------------------------------------------

library(reshape2)
names(Sales)
## [1] "Period"    "Product_A" "Product_B"
Sales_Long <- melt(Sales, id.vars = "Period")
Sales_Long
##    Period  variable value
## 1       1 Product_A    50
## 2       2 Product_A    53
## 3       3 Product_A    57
## 4       4 Product_A    59
## 5       5 Product_A    65
## 6       6 Product_A    67
## 7       7 Product_A    72
## 8       8 Product_A    76
## 9       9 Product_A    79
## 10     10 Product_A    83
## 11     11 Product_A    86
## 12     12 Product_A    93
## 13     13 Product_A    96
## 14      1 Product_B    62
## 15      2 Product_B    66
## 16      3 Product_B    72
## 17      4 Product_B    75
## 18      5 Product_B    79
## 19      6 Product_B    83
## 20      7 Product_B    88
## 21      8 Product_B    92
## 22      9 Product_B    96
## 23     10 Product_B    98
## 24     11 Product_B   102
## 25     12 Product_B   106
## 26     13 Product_B   109
colnames(Sales_Long)[2] <- "Product"
colnames(Sales_Long)[3] <- "Units_Sold"



# 3. Line graph -----------------------------------------------------------

library(ggplot2)

ggplot(data = Sales_Long, aes(x = Period, y = Units_Sold,
                              group = Product,
                              colour = Product)) +
    geom_line() +
    ylab("Number of units sold") +
    scale_x_continuous(breaks = seq(min(Sales_Long$Period), 
                                    max(Sales_Long$Period), by = 2))

# 4. Indexed growth -------------------------------------------------------

Sales
## # A tibble: 13 x 3
##    Period Product_A Product_B
##     <dbl>     <dbl>     <dbl>
##  1      1        50        62
##  2      2        53        66
##  3      3        57        72
##  4      4        59        75
##  5      5        65        79
##  6      6        67        83
##  7      7        72        88
##  8      8        76        92
##  9      9        79        96
## 10     10        83        98
## 11     11        86       102
## 12     12        93       106
## 13     13        96       109
Sales$Product_A_Growth <- Sales$Product_A/50*100
Sales$Product_B_Growth <- Sales$Product_B/62*100
Sales
## # A tibble: 13 x 5
##    Period Product_A Product_B Product_A_Growth Product_B_Growth
##     <dbl>     <dbl>     <dbl>            <dbl>            <dbl>
##  1      1        50        62              100         100.0000
##  2      2        53        66              106         106.4516
##  3      3        57        72              114         116.1290
##  4      4        59        75              118         120.9677
##  5      5        65        79              130         127.4194
##  6      6        67        83              134         133.8710
##  7      7        72        88              144         141.9355
##  8      8        76        92              152         148.3871
##  9      9        79        96              158         154.8387
## 10     10        83        98              166         158.0645
## 11     11        86       102              172         164.5161
## 12     12        93       106              186         170.9677
## 13     13        96       109              192         175.8065
names(Sales)
## [1] "Period"           "Product_A"        "Product_B"       
## [4] "Product_A_Growth" "Product_B_Growth"
Sales_Growth <- subset(Sales, select = c("Period", "Product_A_Growth",
                                         "Product_B_Growth"))
Sales_Growth
## # A tibble: 13 x 3
##    Period Product_A_Growth Product_B_Growth
##     <dbl>            <dbl>            <dbl>
##  1      1              100         100.0000
##  2      2              106         106.4516
##  3      3              114         116.1290
##  4      4              118         120.9677
##  5      5              130         127.4194
##  6      6              134         133.8710
##  7      7              144         141.9355
##  8      8              152         148.3871
##  9      9              158         154.8387
## 10     10              166         158.0645
## 11     11              172         164.5161
## 12     12              186         170.9677
## 13     13              192         175.8065
Sales_Growth_Long <- melt(Sales_Growth, id.vars = "Period")
Sales_Growth_Long
##    Period         variable    value
## 1       1 Product_A_Growth 100.0000
## 2       2 Product_A_Growth 106.0000
## 3       3 Product_A_Growth 114.0000
## 4       4 Product_A_Growth 118.0000
## 5       5 Product_A_Growth 130.0000
## 6       6 Product_A_Growth 134.0000
## 7       7 Product_A_Growth 144.0000
## 8       8 Product_A_Growth 152.0000
## 9       9 Product_A_Growth 158.0000
## 10     10 Product_A_Growth 166.0000
## 11     11 Product_A_Growth 172.0000
## 12     12 Product_A_Growth 186.0000
## 13     13 Product_A_Growth 192.0000
## 14      1 Product_B_Growth 100.0000
## 15      2 Product_B_Growth 106.4516
## 16      3 Product_B_Growth 116.1290
## 17      4 Product_B_Growth 120.9677
## 18      5 Product_B_Growth 127.4194
## 19      6 Product_B_Growth 133.8710
## 20      7 Product_B_Growth 141.9355
## 21      8 Product_B_Growth 148.3871
## 22      9 Product_B_Growth 154.8387
## 23     10 Product_B_Growth 158.0645
## 24     11 Product_B_Growth 164.5161
## 25     12 Product_B_Growth 170.9677
## 26     13 Product_B_Growth 175.8065
colnames(Sales_Growth_Long)[2] <- "Product"
colnames(Sales_Growth_Long)[3] <- "Sales_Growth"

names(Sales_Growth_Long)
## [1] "Period"       "Product"      "Sales_Growth"
ggplot(Sales_Growth_Long, aes(x = Period, y = Sales_Growth,
                              group = Product,
                              colour = Product)) +
    geom_line() +
    ylab("Sales growth") +
    scale_x_continuous(breaks = seq(min(Sales_Long$Period), 
                                    max(Sales_Long$Period), by = 2))