
GENERALIZING THIS FILE TO 
Plot_Users_By_Country_and_Product.r


SE_countries <- c("SE", "NO", "DK", "AU", "US", "GB")
SE_countries <- c("SE", "NO", "DK", "AU", "GB")


## OLD:  SE_USERS1 <- DT.revshare[month > "2011-01-01" & country_code %in% SE_countries][, lapply(.SD, sumn), by=list(month, country_code), .SD=c("registered_users", "active_users")][registered_users != 0]
SE_USERS <- DT.userCounts[month > "2011-01-01" & country_code %in% SE_countries][, lapply(.SD, sumn), by=list(month, country_code, product_description), .SD=c("registered_users", "active_users")][registered_users != 0]
addCountry.byCode_(SE_USERS)

SE_USERS.noproduct <- aggregateDT(SE_USERS, exclude="product_description")

## Add text labels for the graph
SE_USERS[month %in% seq.Date(as.Date("2012-10-01"), by="1 year", length.out=4)
    , c("label_reg", "label_act") := lapply(.SD, function(x) sprintf("%2.01f M", x/1e6))
    , .SDcols = c("registered_users", "active_users")
]
SE_USERS.noproduct[month %in% seq.Date(as.Date("2012-10-01"), by="1 year", length.out=4)
    , c("label_reg", "label_act") := lapply(.SD, function(x) sprintf("%2.01f M", x/1e6))
    , .SDcols = c("registered_users", "active_users")
]


P.registed_overall <- 
{
  ggLinegraph(SE_USERS.noproduct, x="month", y="registered_users", color=c(Country = "country_name"), yscale="millions"
    # , title="Spotify Registered Users by Country (All Products)"
    # , title=gg_text("Total (All Products)", .8)
    , title="Total (All Products)"
    , title_size=1.0
    , ylab_append="Labeled at October of each year"
    , facet_scales = "free_y", xlab=""
    , legend="bottom", legend_font_size=.65) + 
    geom_text(data=SE_USERS.noproduct[!is.na(label_reg)], aes(x=month, y=registered_users * ifelse(registered_users > 1e7, 1.05, 1.1), label=label_reg), color="black", size=3)
}

P.registed_overall.by_product <- 
{
  ggLinegraph(SE_USERS, x="month", y="registered_users", color=c(Country = "country_name"), yscale="millions"
    # , title="Spotify Registered Users by Country and Product"
    , facet_formula="product_description ~.", facet_scales = "free_y", xlab="", legend="off"
    , ylab = ""
    # , ylab_append="Labeled at October of each year"
     ) + 
    geom_text(data=SE_USERS[!is.na(label_reg)], aes(x=month, y=registered_users * ifelse(registered_users > 1e7, 1.05, 1.1), label=label_reg), color="black", size=3)
}



## Useful footer text
sub.txt <- "Only user count for primary products are shown -- bundled products are excluded."
sub.exp <- sprintf('expression(atop("%s", atop(italic("%s"), "")))', txt, gsub("\\n", "", orchardFootNote()$label))
sub <- gg_text(eval(parse(text=sub.exp)), .8)

## PLOT THEM TOGETHER
grid.arrange(P.registed_overall, P.registed_overall.by_product, ncol=2, main="Spotify Registered Users", sub=sub)

