#================================================================================================================# #========== Jacek Wallusch ==========# #========== CIMA: Introduction to Data Analytics ==========# #========== Adding Dimensions to Scatter Plots ==========# #================================================================================================================# # Data: normally distributed with various averages and std. deviations: cust_1 <- c(rnorm(50, mean = 4, sd = 0.25), rnorm(50, mean = 5, sd = 0.25), rnorm(25, mean = 6, sd = 0.25)) cust_2 <- rnorm(125, mean = 5, sd = 0.5) seq_transactions <- seq(1, NROW(cust_1), by = 1) #------------------------------------------------------------------------------------------------------------------------ # First Round: Isolate an individual trace plot_ly(x = seq_transactions, y = cust_1, type = "scatter", mode = "markers", name = "Customer 1", marker = list(color = ifelse(cust_1 < 4.5, "green", ifelse(cust_1 > 4.5 & cust_1 < 5.5, "yellow", "red")), line = list(color = "black", width = 1)))%>% add_trace(x = seq_transactions, y = cust_2, y = cust_1, type = "scatter", mode = "markers", name = "Customer 2", marker = list(color = ifelse(cust_2 < 4.5, "green", ifelse(cust_2 > 4.5 & cust_2 < 5.5, "yellow", "red")), line = list(color = "black", width = 1)))%>% layout(title = "Discount Volume per Transaction", xaxis = list(title = "Transactions"), yaxis = list(title = "Discount Volume in \u00A3 1 000"), margin = list(b = 150, t = 150, l = 150, r = 150, pad = 1)) #------------------------------------------------------------------------------------------------------------------------ # Second Round: Add fourth dimension (shape) plot_ly(x = seq_transactions, y = cust_1, type = "scatter", mode = "markers", name = "Customer 1", marker = list(color = ifelse(cust_1 < 4.5, "green", ifelse(cust_1 > 4.5 & cust_1 < 5.5, "yellow", "red")), line = list(color = "black", width = 1), symbol = "x"))%>% add_trace(x = seq_transactions, y = cust_2, y = cust_1, type = "scatter", mode = "markers", name = "Customer 2", marker = list(line = list(color = ifelse(cust_2 < 4.5, "green", ifelse(cust_2 > 4.5 & cust_2 < 5.5, "yellow", "red")), width = 2), color = "white", symbol = "diamond"))%>% layout(title = "Discount Volume per Transaction", xaxis = list(title = "Transactions"), yaxis = list(title = "Discount Volume in \u00A3 1 000"), margin = list(b = 150, t = 150, l = 150, r = 150, pad = 1)) #------------------------------------------------------------------------------------------------------------------------ # Third Round: Graph descriptive statistics avg_c1 <- mean(cust_1) avg_c2 <- mean(cust_2) repAc1 <- rep(avg_c1, NROW(cust_1)) repAc2 <- rep(avg_c2, NROW(cust_2)) plot_ly(x = seq_transactions, y = cust_1, type = "scatter", mode = "markers", name = "Customer 1", marker = list(color = ifelse(cust_1 < 4.5, "green", ifelse(cust_1 > 4.5 & cust_1 < 5.5, "yellow", "red")), line = list(color = "black", width = 1), symbol = "x"))%>% add_trace(x = seq_transactions, y = cust_2, y = cust_1, type = "scatter", mode = "markers", name = "Customer 2", marker = list(line = list(color = ifelse(cust_2 < 4.5, "green", ifelse(cust_2 > 4.5 & cust_2 < 5.5, "yellow", "red")), width = 2), color = "white", symbol = "diamond"))%>% add_trace(x = seq_transactions, y = repAc1, type = "scatter", mode = "lines+markers", name = "Avg. Cust. 1", inherit = FALSE, line = list(color = "black", width = 1, dash = "dash"), marker = list(symbol = "x"))%>% add_trace(x = seq_transactions, y = repAc2, type = "scatter", mode = "lines+markers", name = "Avg. Cust. 2", inherit = FALSE, line = list(color = "black", width = 1, dash = "dash"), marker = list(symbol = "diamond"))%>% layout(title = "Discount Volume per Transaction", xaxis = list(title = "Transactions"), yaxis = list(title = "Discount Volume in \u00A3 1 000"), margin = list(b = 150, t = 150, l = 150, r = 150, pad = 1))