This file merges DB.all.count 

## Given DB.all.countmeta  (the merge of DB.all.meta & DB.all.count)


metCols <- grep("^(itune|spot)_", names(DB.all.countmeta), value=TRUE)

DB.SUMS <- DB.all.countmeta[, lapply(.SD, sum, na.rm=TRUE), .SDcols=metCols, keyby=list(genre, user_country)]


stores <- c("itune", "spot")
mets <- list()
mets$itune <- grep("^itune_", names(DB.SUMS), value=TRUE)
mets$spot  <- grep("^spot_", names(DB.SUMS), value=TRUE)


DB.SUMS.LONG <- 
  rbind(
     setnames(copy(DB.SUMS[, c(.SD, list(store="Spotify")), .SDcols=c(key(DB.SUMS), mets$spot)]), mets$spot, gsub("^(itune|spot)_", "", mets$spot)) 
   , setnames(copy(DB.SUMS[, c(.SD, list(store="iTunes" )), .SDcols=c(key(DB.SUMS), mets$itune)]), mets$itune, gsub("^(itune|spot)_", "", mets$itune)) 
  )

DB.SUMS.LONG[, countrySum := round(sum(t_units_12weeksending_sep_by_upc_and_country, na.rm=TRUE), -3), by=user_country]
DB.SUMS.LONG[, table(countrySum)]
DB.SUMS.LONG <- DB.SUMS.LONG[countrySum > 750000]
c("artist", "artist_country", "genre", "itune_t_units_12weeksending_aug_by_upc_and_country",
"itune_t_units_12weeksending_jul_by_upc_and_country", "itune_t_units_12weeksending_sep_by_upc_and_country",
"itune_u_customers_12weeksending_aug_by_upc_and_country", "itune_u_customers_12weeksending_jul_by_upc_and_country",
"itune_u_customers_12weeksending_sep_by_upc_and_country", "label",
"label_country", "label_priority", "mkt_priority", "product_type_id",
"release", "releaseDate", "spot_t_units_12weeksending_aug_by_upc_and_country",
"spot_t_units_12weeksending_jul_by_upc_and_country", "spot_t_units_12weeksending_sep_by_upc_and_country",
"spot_u_customers_12weeksending_aug_by_upc_and_country", "spot_u_customers_12weeksending_jul_by_upc_and_country",
"spot_u_customers_12weeksending_sep_by_upc_and_country", "upc",
"user_country")

library(ggplot2)

P.SUMS.1 <- 
ggplot(DB.SUMS.LONG, aes(x=user_country, color=genre, y=t_units_12weeksending_sep_by_upc_and_country, shape=store)) + 
    geom_point() + scale_y_log10() + ggtitle("Total Units by Country")

P.SUMS.2 <- 
ggplot(DB.SUMS.LONG, aes(x=genre, y=user_country, size=t_units_12weeksending_sep_by_upc_and_country, color=store, alpha=0.75)) + 
    geom_jitter() + ggtitle("Size is Total Units\n(by Genre, Country & Store")

# P1 <- 
# ggplot(DB.SUMS.LONG, aes(x=user_country, color=genre, y=t_units_12weeksending_sep_by_upc_and_country, shape=store)) + 
#     geom_point() + scale_y_log10() + ggtitle("Total Units by Country")
# 
# P2 <- 
# ggplot(DB.SUMS.LONG, aes(x=genre, y=user_country, size=t_units_12weeksending_sep_by_upc_and_country, color=store)) + 
#     geom_point() aes(y=spot_t_units_12weeksending_sep_by_upc_and_country), shape=17) + 
#     geom_point(aes(y=itune_t_units_12weeksending_sep_by_upc_and_country), shape=16) +
#     scale_y_log10()
# 


  f.out1 <- as.path(outDir, "GenreAggregates_01.pdf")
  pdf(file=f.out1, width=14, height=7)
  print(P.SUMS.1)
  print(P.SUMS.2)
  dev.off()
  reproduce(f.out1)

