Guest User

Untitled

a guest
Jun 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. library(dplyr); library(tidyr); library(ggplot2)
  2.  
  3. # Read in data
  4. eu_chats <- read.csv("20180601_20180607_EU.csv", sep = ",", stringsAsFactors = FALSE)
  5.  
  6. # Rename columns
  7. colnames(eu_chats) <- c("date", "type", "retailer_code", "count")
  8.  
  9. # Remove time from date column
  10. eu_chats$date <- gsub(", 00:00:00.000", "", eu_chats$date)
  11. eu_chats$date <- gsub("st", "", eu_chats$date)
  12. eu_chats$date <- gsub("nd", "", eu_chats$date)
  13. eu_chats$date <- gsub("rd", "", eu_chats$date)
  14. eu_chats$date <- gsub("th", "", eu_chats$date)
  15.  
  16. eu_chats$date <- as.Date(eu_chats$date, format='%B %d %Y')
  17.  
  18. # Label missed anc completed chats accordingly
  19. eu_chats$type[eu_chats$type == "conversation-auto-archived"] <- "Missed"
  20. eu_chats$type[eu_chats$type == "conversation-archived"] <- "Completed"
  21.  
  22. # Add new columns (intialise to 0 or "retailer")
  23. eu_chats$retailer <- ""
  24.  
  25. # Identify France, Germany & UK stores
  26. eu_chats$retailer[eu_chats$retailer_code == "npqPjZyMy5"] <- "Retailer1"
  27. eu_chats$retailer[eu_chats$retailer_code == "HbNaIqdedB"] <- "Retailer2"
  28. eu_chats$retailer[eu_chats$retailer_code == "1mRdYODJBH"] <- "Retailer3"
  29. eu_chats$retailer[eu_chats$retailer_code == "GGdwO3HFDV"] <- "Retailer4"
  30. eu_chats$retailer[eu_chats$retailer_code == "Tj8vwJvyH1"] <- "Retailer5"
  31. eu_chats$retailer_code <- NULL
  32.  
  33. # Visualise chats
  34.  
  35. eu_chats %>%
  36. spread(type, count, fill = 0) %>% # Spread the count column in missed and completed
  37. mutate(Total = Completed + Missed) %>% # Create the Total column
  38. ggplot(aes(as.Date(date, tz = "Europe/London"), Total)) +
  39. geom_col(aes(fill = "Total"),
  40. colour = "black", width = 0.75) + # total bar (with stat = "identity")
  41. geom_col(aes(y = Missed, fill = "Missed"),
  42. colour = "black", width = 0.75) + # missed bar
  43. geom_text(aes(label = paste("Total chats:", Total)), # add total label
  44. hjust = -0.05, vjust = 0.7, size = 3.5) +
  45. geom_text(aes(label = paste("Missed chats:", Missed, "(", round(Missed/Total*100, 2), "%)")), # add missed label and calculate percentage
  46. hjust = -0.05, vjust = -0.7, size = 3.5, colour = "red") +
  47. scale_fill_manual(name = "", # Manual fill scale
  48. values = c("Total" = "forestgreen", "Missed" = "red")) +
  49. facet_grid(retailer~.) + # Displayed per retailer
  50. scale_y_continuous(limits = c(0, max(eu_chats$count) * 2)) + # Make labels visible
  51. scale_x_date(date_breaks = "1 day", name = "Date") +
  52. ggtitle(paste("Missed Chats (", min(eu_chats$date), "-", max(eu_chats$date), ")")) +
  53. coord_flip()
Add Comment
Please, Sign In to add comment