USA - long, new edit... 

I dont think I have changed much to this yest and is mostly just scrap work 


c("upc", "user_country", "u_count", "t_count", "store", "month",
"release", "releaseDate", "mkt_priority", "product_type_id",
"artist", "label", "label_priority", "genre", "artist_country",
"label_country")


library(ggplot2)
library(scales)

cls(10)
# Every row in metSplit corresponds to one column in the original DT that will be split
mets <- grep("^(itune|spot)_", names(DB.all.countmeta), value=TRUE)
metSplit <- cbind(mets,  do.call(rbind, strsplit(mets, "_")))
metSplit <- as.data.table(metSplit)
# The bycols no longer applies, because we aggregated the values, and filtered out the countries
# metSplit[, bycols := apply(.SD[, 7:10, with=FALSE], 1, paste, collapse="_")]
metSplit[, c(3, 7:10) := NULL, with=FALSE]
setnames(metSplit, c("orig", "store", "measuring", "description", "month"))
metSplit[, names(metSplit) := lapply(.SD, as.character)]


## GENRE SUMS, JUST IN THE USA
USA <- DB.all.countmeta[user_country=="US", lapply(.SD, sum, na.rm=TRUE), .SDcols=mets, keyby=list(genre)]
USA[is.na(genre), genre := "ZZ"]
missingGenres <- USA[, setdiff(genre, names(genreClean))]
USA[, genre := genreClean[genre]]
USA[is.na(genre), genre := "OTHER"]
USA[, .N, by=genre][order(N)]

# These are teh columns that will be stacked, unchanged
cols.untouched <- setdiff(names(USA), metSplit[["orig"]])

# this is what the stacked of the heterogeneous columns will be called
newName <- "count"


# Go ahead and reshapte
USA.LONG <- rbindlist(
                lapply(seq(nrow(metSplit)), function(i) 
                  # We are simply taking .SD, which is all of the untouched columns + the i'th original column
                  #  and c() it with the i'th row of metSplit.
                  # We use setnames on .SD to change the name of the i'th original column to 'newName'
                  USA[, c(setnames(.SD, metSplit[["orig"]][[i]], newName), metSplit[i, !"orig", with=FALSE])
                     , .SDcols=c(cols.untouched, metSplit[["orig"]][[i]] )]
                )
              )

USA.LONG[, store := factor(store, levels=c("spot", "itune"), labels=c("Spotify", "iTunes"))]
USA.LONG[, month := factor(topropper(month), levels=unique(topropper(month)))] 

USA.LONG <- USA.LONG[genre %ni% c("ZZ", "OTHER")]

P.USA <- 
ggplot(USA.LONG, aes(x=month, y=count, color=genre)) + geom_point() + scale_color_manual(values=colors.genre) + 
    facet_grid(store ~ measuring) + scale_y_log10(breaks=10^(6:8), labels=paste(10^(0:2), "M")) + 
    theme(axis.text.y=element_text(size=rel(1.5))) + 
    ggtitle("USA\nGenre Breakdown for iTunes & Spotify")

f.out <- as.path(outDir, "USA_genre_Spotify+iTunes_noOTHER.pdf")
dir.create(dirname(f.out), showWarnings=FALSE, recursive=TRUE)
ggsave(f.out, plot=P.USA, width=11, height=11)
reproduce(f.out)