## SET UP IF STARTING FROM HERE
{
  setScience(proj="Spotify_Accounting_ETL", subProj="StreamConversion", subl=FALSE)
  lib(bit64)
  lib(reshape2)
  lib(ggplot2)
  loadIfNotExists("DT.Total_usage")
}


## FLAGS
spot_using <- "Spotify"
spot_using <- "Spotify x-Orch"

RatioType_using <- "Ratio_00To30"

### ---------------------------------------------- ###

# DT.Total_usage


# -------------------------------
# OLD : kCols.DUS <- c("month", "Units", "CutOff")
# OLD : kCols.DCgUS <- c("month", "country_code", "Units", "CutOff")
# OLD : kCols.DOUS <- c("month", "Orchard_vs_Spotify", "Units", "CutOff")
# OLD : kCols.ts <- c("country_code", "CutOff", "Units")
# OLD : kCols.US <- c("Units", "CutOff")

## NEW kCols
kCols.DS   <- c("month",          "StreamType")
kCols.US   <- c(         "Units", "StreamType")
kCols.DUS  <- c("month", "Units", "StreamType")
kCols.DO   <- c("month", "Orchard_vs_Spotify" )
kCols.DOC   <- c("month", "Orchard_vs_Spotify", "country_code", "country_name", "region_group")
kCols.DOUS  <- c("month", "Orchard_vs_Spotify", "Units", "StreamType")
kCols.DCgUS <- c("month", "region_group", "Units", "StreamType")

OrchSpotColors <- getDict("Orchard_vs_Spotify")
## MELTED DATA (reshaped in the future)
numericCols <- nwhich(canBeNumeric(DT.Total_usage))
DT.Total_usage[, (numericCols) := lapply(.SD, as.numeric), .SDcols = numericCols]
DT.Total_usage.melt <- melt(DT.Total_usage, id.vars=c("month", "country_code"), value.name="count", measure.vars=numericCols)

## Shorthand 
sDT <- DT.Total_usage.melt


## Add in region_group
addCountryInfo_(sDT)
setcolorderpt(sDT, startCols=c("month", "country_code", "country_name", "region_group"))

## Splice out the column types from the original, unmelted column name
sDT[, Orchard_vs_Spotify := ifelse(grepl("orchard", variable), "Orchard", "Spotify")]
sDT[, Units := ifelse(grepl("minutes", variable), "Minutes", "Streams")]
sDT[Units == "Streams", CutOff := ifelse(grepl("15", variable), 15,  ifelse(grepl("30", variable), 30, 0))]

## Calculate the ABSOLUTE stream count (whereas, the orignal had cumsum counts)
byCols.abs <- c("month", "country_code", "Units", "Orchard_vs_Spotify")
sDT[Units != "Minutes", absolute_count := c(-diff(count), count[CutOff == 30]), by=byCols.abs]
# sDT[Units != "Minutes", absolute_count := {
#   c(
#     t.00_to_15 = count[CutOff == 0] - count[CutOff == 15] # The latter value includes 30_plus
#   , t.15_to_30 = count[CutOff == 15] - count[CutOff == 30]
#   , t.30_plus  = count[CutOff == 30] - 0 # no change
# )} , by=byCols.abs]
## CONFIRM
stopifnot(sDT[Units != "Minutes", absolute_count[CutOff == 0] + count[CutOff == 15] == count[CutOff == 0], by=byCols.abs][, V1])
stopifnot(sDT[Units != "Minutes", absolute_count[CutOff == 15] + absolute_count[CutOff == 30] == count[CutOff == 15], by=byCols.abs][, V1])
stopifnot(sDT[Units != "Minutes", sum(absolute_count) == count[CutOff==0], by=byCols.abs][, V1])

sDT[CutOff == 00, StreamType := "Streams 00-15 Secs"]
sDT[CutOff == 15, StreamType := "Streams 15-30 Secs"]
sDT[CutOff == 30, StreamType := "Streams 30+   Secs"]

sDT[Units == "Minutes", absolute_count := count]
sDT[Units == "Minutes", StreamType := "Total Mins Streamed"]

sDT[, month := as.Date(month)]


## Remove shorthand 
{
  if (identical(DT.Total_usage.melt, sDT))
    rm (sDT)
  else 
    stop ("DT.Total_usage.melt is NOT identical to sDT")
}

## Add in "Total Streams" 
if ("TOTAL STREAMS 0+ Secs" %ni% DT.Total_usage.melt$StreamType) {
  DT.Total_usage.melt <- 
    rbind(  DT.Total_usage.melt
          , DT.Total_usage.melt[Units != "Minutes", list(StreamType = "TOTAL STREAMS 0+ Secs", CutOff = (-1), absolute_count = count[CutOff==0]), keyby=c(setdiff(names(DT.Total_usage.melt), c("StreamType", "CutOff", "count", "variable", "absolute_count")))]
          , fill = TRUE)
}



## 0 Seconds vs 15 seconds vs 30 Seconds
DT.Total_usage.melt.aggdByC <- sumTheTable(DT.Total_usage.melt, byCols=c(kCols.DOUS, "CutOff"), colsUsing="absolute_count")
DT.Total_usage.melt.aggdByC[, What := ifelse(grepl("Total", StreamType, ignore.case=TRUE), "Total", "Absolute Counts")]
DT.Total_usage.melt.aggdByC[, Units := factor(Units, levels=c("Streams", "Minutes"))]
# DT.Total_usage.melt.aggdByC[, Counting := ifelse(CutOff == 0, "00 ~ 15 Seconds [Unpaid]", ifelse(CutOff == 15, " ~ 15 Seconds [Unpaid]",  )

## ADD in Spotify x-Orchard
add_in_xOrchard <- function(DT, orchCol="Orchard_vs_Spotify", measureCols=c("absolute_count", "count", "Value", "value"), exclude=c(), xorch_string="Spotify x-Orch", makefactor=TRUE) {
  calculate_xOrchard <- function(x, OScol) {
    if (length(x) != 2)
      warning (sprintf("%s than 2 elements provided to calculate_xOrchard()", ifelse(length(x) < 2, "Less", "More")))
    x[OScol == "Spotify"] - x[OScol == "Orchard"]
  }
  # browser()
  tmp_measureCols <- c(orchCol, measureCols) %>% intersect(names(DT))
  tmp_dimCols <- setdiff(names(DT), c(tmp_measureCols, exclude))

  ## sanity check
  if (length(tmp_measureCols) == 1 && tmp_measureCols == orchCol)
    stop ("none of the measureCols where in the DT")

  tmp_DT.xOrch <- DT[, {.SD; lapply(.SD[, !orchCol, with=FALSE], calculate_xOrchard, .SD[[orchCol]])}, by=tmp_dimCols, .SDcols=tmp_measureCols] %>% cbind(xorch_string) %>% {setnames(., length(.), orchCol)} %>% {.}
  ## rbind it all together

  ret <- rbind(DT, tmp_DT.xOrch, use.names=TRUE, fill=TRUE)

  if (makefactor)
    ret[, (orchCol) := factor(get(orchCol), levels=c("Orchard", xorch_string, "Spotify"))]

  return(rbind(DT, tmp_DT.xOrch, use.names=TRUE, fill=TRUE))
}

DT.Total_usage.melt %<>% add_in_xOrchard(exclude="variable")

### Question 1:  Is the ratio (00-15) / (30+) Constant? 
DT.ratio_counts <- {
          DT.Total_usage.melt[!is.na(CutOff) & Orchard_vs_Spotify %in% c("Orchard", spot_using), {
               ret <- c( Ratio_00To30 = count[CutOff==30] / count[CutOff==00]
                       , Ratio_15To30 = count[CutOff==30] / count[CutOff==15]
                       , Ratio_00To15 = count[CutOff==15] / count[CutOff==00])
               list(RatioType=names(ret), Value=ret)
             } , keyby=kCols.DOC
             ][RatioType == RatioType_using]
}
ylims <- c(0.55, 0.85, DT.plot$Value, DT.plot_text_words$y) %>% range

# levs.months <- sapply()
fmt_month <- "%Y-%b"
levs <- DT.ratio_counts[, range(month)]  %>% as.list %>% setNames(c("from", "to"))%>% c(by="month") %>% do.call(what=seq.Date, .) %>% format(fmt_month)
DT.ratio_counts[, mm := toFactorWithExpectedLevels(format(month, format=fmt_month), levels=levs)]

DT.ratio_counts.medians <- DT.ratio_counts[, list(median_value=median(Value)), keyby=list(month, mm, Orchard_vs_Spotify)]
DT.ratio_counts.medians_for_segment <- DT.ratio_counts.medians %>% {cbind(.[, head(.SD, -1), keyby=Orchard_vs_Spotify], setnames(copy(.), paste0(names(.), "_end"))[, tail(.SD, -1), keyby=Orchard_vs_Spotify_end])}

DT.ratio_counts.means <- DT.ratio_counts[, list(mean_value=mean(Value)), keyby=list(month, mm, Orchard_vs_Spotify)]
DT.ratio_counts.means_for_segment <- DT.ratio_counts.means %>% {cbind(.[, head(.SD, -1), keyby=Orchard_vs_Spotify], setnames(copy(.), paste0(names(.), "_end"))[, tail(.SD, -1), keyby=Orchard_vs_Spotify_end])}

## Set the key to OvsS and Ratio Type, for easier filtering later
setkeyIfNot(DT.ratio_counts, "Orchard_vs_Spotify", "RatioType", verbose=FALSE)

## TODO: Fix this
DT.ratio_counts[, mgroup := ifelse(month <= "2014-05-31", "2014-Feb to 2014-May", sprintf("2014-Jun to %s", mm[month==max(month)][[1]]))]

## Margins for plot
margins <- c(top=1, right=2, bottom=1, left=1) * 7

rgrps <- DT.ratio_counts$region_group %>% {if (is.factor(.)) levels(.) else unique(.)} %>% c("WW", .)
P.stream_conversions_by_region <- emptylist(rgrps)
for (rgrp in rgrps) {
  ## verbose for user
  catn("~~~~~~~~~ PLOTTING: ", rgrp, " ~~~~~~~~~~")

  ## Crop the data to the region group being used
  DT.ratio_count_for_rgrp <- if (rgrp == "WW") DT.ratio_counts else DT.ratio_counts[(rgrp == region_group)]

  if (!nrow(DT.ratio_count_for_rgrp)) {
    message("No rows for '", rgrp, "'  --  skipping")
    next;
  }

  ## Words for the graph
  TITLE <-  sprintf("STREAM CONVERSIONS [%s]", ifelse(rgrp=="WW", "World Wide", ifelse(rgrp=="ROW", "Rest of World", rgrp))) %>% subtext("from unmonetized-stream to monetized-stream  (ie, longer than 30 seconds)")
  # ylab  <- "Ratio Per Country of     [# of streams lasting longer than 30 Seconds] /\n        [# of streamss initiated]\n"
  ylab  <- "Ratio Per Country of   over(# of streams lasting > 30 Seconds\", \"Total # of streamss initiated)\n"

  if (!identical(DT.ratio_count_for_rgrp[, unique(RatioType)], "Ratio_00To30"))
    stop ("This code not implemented for RatioType_using != 'Ratio_00To30'")

    ## In an older iteration of the code, we were not cropping by RatioType until this point in the code.
    ## In the current iteration of the code, DT.plot is essentially identical to DT.ratio_count_for_rgrp
    DT.plot <- copy(DT.ratio_count_for_rgrp[RatioType == RatioType_using])
    DT.plot_text_numbs <- DT.plot[(month(month)==5), list(median_avg=fwp(median(Value), 0)), by=list(mm, Orchard_vs_Spotify)
                          ][, y := 0.86 + 0.015 * (-1)^(Orchard_vs_Spotify != "Orchard")]
    DT.plot_text_words <- DT.plot[(month(month)==3), list(median_avg=sprintf("Median\nMay '%s: ", year(month[[1]]) %% 100)), by=list(mm, Orchard_vs_Spotify)
                          ][, y := 0.86 + 0.015 * (-1)^(Orchard_vs_Spotify != "Orchard")]

    notch_on <- nrow(DT.plot[(month == max(month)) & Orchard_vs_Spotify == "Orchard"]) > 30

    ## Calculate the Averages, used for plotting the horizontal bar (geom_segment)
    tmp.avgs <- DT.ratio_count_for_rgrp[, median(Value), by=list(Orchard_vs_Spotify, mgroup)]
    tmp.avgs[, c("xmin", "xmax") := as.data.table(do.call(rbind, strsplit(mgroup, " to ")))]

 
  P.stream_conversion <- 
  {
    ## Both Increased, but Spotify Increased more
    ggplot() + 
      geom_boxplot(data=DT.plot[.("Orchard")],  aes(x=mm, y=Value, fill=Orchard_vs_Spotify, color=Orchard_vs_Spotify), alpha=.40, notch=notch_on) + 
      geom_boxplot(data=DT.plot[.(spot_using)], aes(x=mm, y=Value, fill=Orchard_vs_Spotify, color=Orchard_vs_Spotify), alpha=.40, notch=notch_on)  + 
      labs(title=TITLE, y=subtext(main=ylab, subtext=" "))  +
      scale_x_discrete(breaks=levs[seq(2, length(levs), by=2)]) +

      geom_point(data=DT.plot[, list(Value, quantile(Value, probs=.75) + 1.5 * IQR(Value)), keyby=c("mm", "Orchard_vs_Spotify")][Value > V2], aes(x=mm, y=Value, color=Orchard_vs_Spotify)) + 
      geom_point(data=DT.plot[, list(Value, quantile(Value, probs=.25) - 1.5 * IQR(Value)), keyby=c("mm", "Orchard_vs_Spotify")][Value < V2], aes(x=mm, y=Value, color=Orchard_vs_Spotify)) + 

      geom_line(data=DT.plot, aes(x=mm, y=median(Value), fill=Orchard_vs_Spotify, color=Orchard_vs_Spotify)) + 

      angledtext(x=20) + percent.y(limits=ylims) + 
      relativetext(x=0.75, y=1.10, what="text") +
      relativetext(x=1.10, y=0.85, what="title") +
      legendTitleOff() + legendtop() + 
      theme(plot.margin = unit(margins, "mm")) + 

      ## COLORS
      color_by_dict(OrchSpotColors) + fill_by_dict(OrchSpotColors)
  }

  ## I dont like the way this looks; so removing it for now
  if (FALSE) {
    ## Add text for median value this month, and same time last year. 
    geom_text(data=DT.plot_text_numbs, aes(x=mm, y=y, label=median_avg, color=Orchard_vs_Spotify), size=4, fontface="bold") + 
    geom_text(data=DT.plot_text_words, aes(x=mm, y=y, label=median_avg), size=3, color="Black", fontface="bold")
  }

  if (rgrp == "WW") {
    P.stream_conversion <- P.stream_conversion +
      geom_segment(data=tmp.avgs, aes(x = xmin, xend=xmax, y = V1, yend=V1), color="black"            , size=1.5, alpha=0.5) +
      geom_segment(data=tmp.avgs, aes(x = xmin, xend=xmax, y = V1, yend=V1, color=Orchard_vs_Spotify) , size=1,   alpha=1.0) + 
      xlab(subtext("Month", "horizontal line represents the median average for the time frame"))
  } else {
    P.stream_conversion <- 
    P.stream_conversion + 
      geom_segment(data = DT.ratio_counts.medians_for_segment, aes(x=mm, y=median_value, xend=mm_end, yend=median_value_end, color=Orchard_vs_Spotify)) + 
      xlab(subtext("", "horizontal line represents the *worldwide* median average"))
  }

  P.stream_conversions_by_region[[rgrp]] <- P.stream_conversion
}


## SAVE
## For a single plot:
# ggsave.out(P.stream_conversion, open=TRUE, width=8, height=8)
## For multiple plots:
P.stream_conversions_by_region %<>% removeNullsFromList()
printToPDF(P.stream_conversions_by_region, height.per.plot=9, width.per.plot=8, paginate=TRUE, open=TRUE)

print(ggsave.out(plot=P.stream_conversions_by_region[["WW"]], nm="Stream Conversions Worldwide", ext="png"))

"&&& LEFT OFF HERE .... "
"TODO :: marrange to group of 3"


#   |
#   |## Different Plot type
#   |# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#   |  DT.Total_usage.melt.aggdByC %<>% add_in_xOrchard()
#   |  P <- 
#   |  {
#   |    ggplot(DT.Total_usage.melt.aggdByC, aes(x=month, y=absolute_count, color=StreamType)) +
#   |      geom_line(aes(linetype=Units), size=1.4) + 
#   |      facet_grid(What + Orchard_vs_Spotify ~ ., scale="free_y") + 
#   |      millions.y() + ylab("Count in Millions") + 
#   |      ggtitle("Usage by Month (Minutes or Streams) -- Orchard vs Spotify")
#   |  }
#   |
#   |  P2 <- colorByGroup(P, OrchSpotColors, showWarnings=FALSE)
#   |  plot(P2)
#   |# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#   |
#   |
#   |
#   |
#   |
#   |AFTER THIS IS SCRATCH WORK FROM A YEAR AGO...
#   |CHECK IF IT HAS ANYTHING MEANINGUFL, THEN KILL IT
#   |
#   |# This whole file used to be called 
#   |# ~/git/orch/src/Spotify_Accounting_ETL/02b DT.data.Total_usage - absolute.r
#   |# And relates to 
#   |#   ~/git/orch/src/Spotify_Accounting_ETL/02 Ingest Spotify Files.r
#   |#   etc.
#   |
#   |
#   |
#   |
#   |
#   |
#   |
#   |
#   |-------------------------------
#   |## update 2015-08-27 
#   |## I am not sure what the purpose to DT.ratio_counts2 was? 
#   |## I was comparing it to DT.ratio_counts for some reason
#   |if (FALSE)
#   |{
#   |  DT.ratio_counts2 <-DT.Total_usage[, list(Orchard_Ratio_00To30 = orchard_streams_above_30_sec / orchard_streams
#   |                                                , Spotify_Ratio_00To30 = all_streams_above_30_sec / all_streams)
#   |                                        , keyby=list(month, country_code)]
#   |  DT.ratio_counts2 <- melt(DT.ratio_counts2, id.vars=key(DT.ratio_counts2))
#   |  DT.ratio_counts2[, Orchard_vs_Spotify := ifelse(grepl("Orchard", variable), "Orchard", "Spotify")]
#   |  DT.ratio_counts2[, variable := gsub(".*\\.", "", variable)]
#   |  setnames(DT.ratio_counts2, "variable", "RatioType")
#   |  setnames(DT.ratio_counts2, "value", "Value")
#   |  matchKey(DT.ratio_counts, DT.ratio_counts2, kCols.DOC, organize=TRUE)
#   |  identical(DT.ratio_counts[RatioType == "Ratio_00To30"]$Value, DT.ratio_counts2$Value)
#   |}
#   |
#   |-------------------------------
#   |### -------- PAID VS UNPAID -------------------- ###
#   |## START BY dcasting
#   |DT.Total_usage.dcast <- dcast.data.table(DT.Total_usage.melt[TRUE][,StreamType := ifelse(grepl("15", StreamType), "Streams 00-30 Secs", StreamType)], month + Units + StreamType ~ Orchard_vs_Spotify, value.var="absolute_count", fun.aggregate=sum)
#   |DT.Total_usage.dcast <- DT.Total_usage.dcast[!grepl("Total Mins", StreamType)]
#   |DT.Total_usage.dcast[, StreamType := ifelse(grepl("00", StreamType), "Unpaid Streams", ifelse(grepl("TOTAL", StreamType), "TOTAL Streams", "Paid Streams"))]
#   |DT.Total_usage.dcast[, StreamType := factor(StreamType, levels=c("Unpaid Streams", "Paid Streams", "TOTAL Streams"))]
#   |setkeyIfNot(DT.Total_usage.dcast, kCols.DUS, verbose=FALSE, organize=TRUE)
#   |
#   |## Add ratio column
#   |DT.Total_usage.dcast[, orch_to_spot_ratio := Orchard / Spotify]
#   |
#   |### -------------------
#   |TITLE <- "Number of Stream (or Total Minutes)\nComparing Orchard to Spotify, by Month and Stream cut-off" 
#   |P.orchard_to_spotify_ratio_paid_unpaid <-  
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=StreamType)) + geom_line(size=1, aes(linetype=StreamType)) + geom_point(alpha=.3, size=2.5) + facet_grid(Units ~ ., scale="free_y") + percent.y() + labs(y="Ratio of Orchard to Spotify, by group\n", title=subtext("Orchard Streams as a Percentage of Spotify Streams", "NOTE: Orchard includes RED")) + scale_linetype_manual(values=c(1, 1, 2))
#   |ggsave.out(P.orchard_to_spotify_ratio_paid_unpaid, open=TRUE)
#   |### -----------------------------------------------------------
#   |-------------------------------
#   |
#   |
#   |## START BY dcasting
#   |DT.Total_usage.dcast <- dcast.data.table(DT.Total_usage.melt, month + Units + StreamType ~ Orchard_vs_Spotify, value.var="absolute_count", fun.aggregate=sum)
#   |
#   |setkeyIfNot(DT.Total_usage.dcast, kCols.DUS, verbose=FALSE, organize=TRUE)
#   |
#   |## Add ratio column
#   |DT.Total_usage.dcast[, orch_to_spot_ratio := Orchard / Spotify]
#   |DT.Total_usage.dcast[, DELTA_orch_to_spot_ratio := diffNA(orch_to_spot_ratio), by=kCols.US]
#   |
#   |## Percent of increase
#   |DT.Total_usage.dcast[, Orch.perc_change := percentIncrease(Orchard), by=kCols.US]
#   |DT.Total_usage.dcast[, Spot.perc_change := percentIncrease(Spotify), by=kCols.US]
#   |DT.Total_usage.dcast[, DELTA_Orch.perc_change := diffNA(Orch.perc_change), by=kCols.US]
#   |DT.Total_usage.dcast[, DELTA_Spot.perc_change := diffNA(Spot.perc_change), by=kCols.US]
#   |
#   |## SEE 
#   |DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")))][order(StreamType)]
#   |DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")))][order(StreamType)]
#   |
#   |
#   |TITLE <- "Number of Stream (or Total Minutes)\nComparing Orchard to Spotify, by Month and Stream cut-off" 
#   |## OLD
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + facet_grid(Units ~ ., scale="free_y") + percent.y()
#   |
#   |## same, no minutes
#   |### -------------------
#   |P.orchard_to_spotify_ratio <-  ggplot(DT.Total_usage.dcast[Units != "Minutes"], aes(x=month, y=orch_to_spot_ratio, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + facet_grid(Units ~ ., scale="free_y") + percent.y() + labs(y="Ratio of Orchard to Spotify, by group\n", title=subtext("Orchard Streams as a Percentage of Spotify Streams", "NOTE: Orchard includes RED"))
#   |ggsave.out(P.orchard_to_spotify_ratio)
#   |### -------------------
#   |
#   |
#   |ggplot(DT.Total_usage.dcast[Units != "Minutes"], aes(x=month, y=DELTA_orch_to_spot_ratio, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y() + geom_hline(y=0, color="white", linetype=1, size=1.2, alpha=.8) + geom_hline(y=0, color="black", size=.75, linetype=4, alpha=.6)
#   |
#   |## NEW 20141020
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y() + labs(title=TITLE, y="Orchard as a percent of Spotify")
#   |
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=DELTA_orch_to_spot_ratio, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y() + labs(title=paste0("CHANGE IN ", TITLE), y="CHANGE IN Orchard as a percent of Spotify")
#   |
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=DELTA_Orch.perc_change, color=StreamType)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y() + labs(title=paste0("CHANGE IN ", TITLE), y="CHANGE IN Orchard as a percent of Spotify")
#   |dev.new()
#   |
#   |---------------------------------------
#   |## Same as above but with country_code added
#   |## START BY dcasting
#   |DT.Total_usage.dcast <- dcast.data.table(DT.Total_usage.melt, month + country_code + Units + CutOff ~ Orchard_vs_Spotify, value.var="count", fun.aggregate=sum)
#   |
#   |setkeyIfNot(DT.Total_usage.dcast, kCols.DCgUS, verbose=FALSE, organize=TRUE)
#   |
#   |## Add ratio column
#   |DT.Total_usage.dcast[, orch_to_spot_ratio := Orchard / Spotify]
#   |
#   |## SEE 
#   |DT.Total_usage.dcast[.(as.Date("2014-07-01"), "US")]
#   |
#   |## Scale Uniform to [0, 1]
#   |# DT.Total_usage.dcast[, Orch.scaled_01 := scaleunif(Orchard), by=kCols.ts]
#   |# DT.Total_usage.dcast[, Spot.scaled_01 := scaleunif(Spotify), by=kCols.ts]
#   |
#   |## Scale to SD
#   |DT.Total_usage.dcast[, Orch.scaled := scale(Orchard), by=kCols.ts]
#   |DT.Total_usage.dcast[, Spot.scaled := scale(Spotify), by=kCols.ts]
#   |
#   |## Percent of increase
#   |DT.Total_usage.dcast[, Orch.perc_change := percentIncrease(Orchard), by=kCols.ts]
#   |DT.Total_usage.dcast[, Spot.perc_change := percentIncrease(Spotify), by=kCols.ts]
#   |
#   |## SEE 
#   |DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")), "US")][order(CutOff)]
#   |DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")), "UY")][order(CutOff)]
#   |
#   |
#   |ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=factor(CutOff))) + geom_line() + facet_grid(Units ~ ., scale="free_y") 
#   |
#   |---------------------------------------
#   |kCols.region_group <- gsub("^country_code$", "region_group", kCols.DCgUS)
#   |DT.plot.agg <- melt(DT.Total_usage.dcast[!is.na(CutOff) & CutOff == 30], id.vars=kCols.DCgUS)[, variable := as.character(variable)]
#   |DT.plot.agg[, Orchard_vs_Spotify := ifelse(grepl("orch", as.character(variable), ignore.case=TRUE), "Orchard", "Spotify")]
#   |addColsFrom_(DT.plot.agg, DT.country, joinCols="country_code")
#   |DT.plot.agg <- DT.plot.agg[region_group != "Canada"]
#   |DT.plot.agg <- DT.plot.agg[grepl("perc_change$", variable), lapply(.SD, sumn), keyby=c(kCols.region_group, "Orchard_vs_Spotify"), .SDcols="value"]
#   |
#   |DT.plot.agg[.(as.Date("2014-04-01"))]
#   |
#   |y.var <- "value"
#   |ggplot(data = DT.plot.agg[month > min(month)], aes_string(x="month", y=y.var, color="region_group", linetype="Orchard_vs_Spotify")) + geom_line() + geom_point(alpha=.5) +  ylab(paste0(gsub("_", " ", y.var), " (percent)"))
#   |
#   |+ facet_grid(type_of_trial ~ ., scales="free_y") 
#   |---------------------------------------
#   |
#   |DT.plot.agg <- melt(DT.Total_usage.dcast[!is.na(CutOff) & CutOff == 30], id.vars=kCols.DCgUS)[, variable := as.character(variable)]
#   |DT.plot.agg[, Orchard_vs_Spotify := ifelse(grepl("orch", as.character(variable), ignore.case=TRUE), "Orchard", "Spotify")]
#   |addColsFrom_(DT.plot.agg, DT.country, joinCols="country_code")
#   |
#   |y.var <- "value"
#   |ggplot(data = DT.plot.agg[grepl("scaled_01", variable)], aes_string(x="month", y=y.var, color="region_group", linetype="Orchard_vs_Spotify")) + geom_line() + geom_point(alpha=.5) +  ylab(paste0(gsub("_", " ", y.var), " (percent)")) 
#   |
#   |+ facet_grid(type_of_trial ~ ., scales="free_y") 
#   |---------------------------------------
#   |
#   |
#   |whose <- c(orchard="orchard_streams", all="all_streams")
#   |length <- c(touched="", short="_above_15_sec", full="_above_30_sec")
#   |
#   |## ------------------------------------ ##
#   |##      STILL TODO                      ## 
#   |## ------------------------------------ ##
#   |    for (l in length)
#   |      DT.Total_usage[, paste0("orch_to_all_ratio", l) := get(paste0("orchard_streams", l)) / get(paste0("all_streams", l))]
#   |    DT.Total_usage[, orch_to_all_ratio_minutes := orchard_minutes_streamed / minutes_streamed]
#   |    ##
#   |    DT.Total_usage[, perc.songs_skipped := 1 - all_streams_above_30_sec/all_streams]
#   |    DT.Total_usage[, perc.orchard_songs_skipped := 1 - orchard_streams_above_30_sec/orchard_streams]
#   |## ------------------------------------
#   |
#   |addColsFrom_(DT.Total_usage, DT.country, joinCols="country_code")
#   |
#   |------------------------------------------------
#   |
#   |
#   |plotSpotifyData <- function(DT, y.vars=c("perc.songs_skipped"))
#   |## Look at USA vs ROW
#   |DT.plot <- copy(DT.Trial_uptake)
#   |y.var = "No._of_trials_starting_during_the_month"
#   |
#   |addColsFrom_(DT.plot, DT.country, joinCols="country_code")
#   |
#   |byCols <- c(setdiff(key(DT.plot), "country_code"), "region_group")
#   |DT.plot[, country_code := NULL]
#   |
#   |
#   |## Aggregate
#   |DT.plot.agg <- DT.plot[, lapply(.SD, sumn), keyby=byCols]
#   |## Drop any type that only has one or two data points
#   |DT.plot.agg <- DT.plot.agg[type_of_trial %in% DT.plot.agg[, .N, by=type_of_trial][N > 2, type_of_trial]]
#   |
#   |lib(ggplot2)
#   |ggplot(data = DT.plot.agg, aes_string(x="month", y=y.var, color="region_group")) + geom_line() + facet_grid(type_of_trial ~ ., scales="free_y") + millions.y() + ylab(paste0(gsub("_", " ", y.var), " (in millions)"))
#   |----------------------------
