# 05 Plotting.r

library(ggplot2)
library(scales)
library(gridExtra)

files.out <- c()

DT.Spotify.Counts.agg[, Date := as.Date(sprintf("%s-%s-01", year, month))]
DT.delivered.Spotify[, Date := as.Date(sprintf("%s-%s-01", year, month))]

DT.using.delv <- DT.delivered.Spotify[Date >= '2013-03-01' & Date <= '2014-01-01']
DT.using.agg <- DT.Spotify.Counts.agg[Date >= '2013-03-01' & Date <= '2014-01-01']
DT.using.tot <- unique(DT.using.agg[, list(Date, Total_Monthly_Streams, Total_Monthly_Users)], by=c("Date"))

Plots <- list()

Plots[["unique_users"]] <- 
ggplot(DT.using.agg, aes(x=Date)) + 
   geom_line(aes(y=Unique_Users, color=product)) + xlab("Month") + 
   scale_y_continuous(labels=comma) + 
   # scale_y_continuous(labels=comma) + 
   ggtitle("\n Unique Users Per Product Per Month") + theme(legend.position="bottom")


Plots[["streams_users"]] <- 
ggplot(DT.using.agg, aes(x=Date)) + 
   geom_line(aes(y=Streams_per_User, color=product)) + xlab("Month") + 
   scale_y_continuous(labels=comma) + 
   # scale_y_continuous(labels=comma) + 
   ggtitle("Avg Streams Per User Per Product Per Month") + theme(legend.position="bottom")


Plots[["streams"]] <- 
ggplot(DT.using.agg, aes(x=Date)) + 
   geom_line(aes(y=Total_Streams, color=product)) + xlab("Month") + 
   scale_y_continuous(labels=comma) + 
   # scale_y_continuous(labels=comma) + 
   ggtitle("Total Streams Per Product Per Month") + theme(legend.position="bottom")


Plots[["Total.Users"]] <- 
        ggplot(data=DT.using.tot) + geom_line(aes(x=Date, y=Total_Monthly_Users)) + xlab("Month") + 
           ggtitle("Total Monthly Users per Month") + 
           scale_y_continuous(labels=comma)

Plots[["Total.Streams"]] <- 
        ggplot(data=DT.using.tot) + geom_line(aes(x=Date, y=Total_Monthly_Streams)) + xlab("Month") + 
           ggtitle("Total Monthly Streams per Month") + 
           scale_y_continuous(labels=comma)

Plots[["delv"]] <- ggplot(data=DT.using.delv) + 
           geom_line(aes(x=as.Date(sprintf("%s-%s-01", year, month)), y=DeliveredTracks   )) + xlab("Month") + 
           ggtitle("\nNumber of Delivered Tracks per Month\n from The Orchard to Spotify") + 
           scale_y_continuous(labels=comma)

main <- textGrob("\nSpotify Activity from July to Dec of 2013\n", gp=gpar(cex=1.5))
files.out <- printToPDF(Plots, "Spotify Streams Per User", height=5, ncol=1, open=TRUE, fouts=files.out)

#### ========================================================================  #####


DT.six.month.avg <- DT.using.agg[Date >= "2013-07-01" & Date <= "2013-12-31"
                                , list(AvgStreamPerUser=sum(Total_Streams) / sum(Unique_Users)
                                       , AvgMonthlyUsers=mean(Unique_Users))
                                , keyby=product]


## Clean up product labels
dict.product <- getDict.SpotifyProduct()
dict.product2 <- setNames(nm=names(dict.product))
names(dict.product2)[names(dict.product2)=="DesktopBasic"] <- "Desktop Basic\n(\"Unlimited\")"
DT.six.month.avg[, product := makeFactorUsingDict.quick(product, dict.product2)]


P.base <- ggplot(data=DT.six.month.avg, aes(x=product, fill=product)) + 
            scale_y_continuous(labels=comma) + xlab("Product\n") + 
            # theme(plot.margin=unit(c(top=.01,right=.01,bottom=.02,.31), "npc")) +
            .nolegend


PlotsAvgs <- list(
    P.base + geom_bar(aes(y=AvgStreamPerUser), stat="identity", width=.5) + 
             labs(title="Spotify Average Number of Monthly Streams Per User", y="Average Number of\nStreams Per User in a Month\n") + 
             geom_text(aes(label=round(AvgStreamPerUser), y=AvgStreamPerUser+.02*max(AvgStreamPerUser, na.rm=TRUE)), size=4)
    ,
    P.base + geom_bar(aes(y=AvgMonthlyUsers), stat="identity", width=.5) + 
             labs(title="Spotify Average Number of Monthly Users", y="Average Number of\nUsers in a Month\n") + 
             geom_text(aes(label=formnumb(selfRound(AvgMonthlyUsers)), y=AvgMonthlyUsers+.02*max(AvgMonthlyUsers, na.rm=TRUE)), size=4)
  )


main <- textGrob("\nSpotify Activity from July to Dec of 2013\n", gp=gpar(cex=1.5))
files.out <- printToPDF(PlotsAvgs, "Spotify Users and Streams", height=6, main=main, sub=orchardFootNote(), fouts=files.out)

files.out <- printToPDF(Plots, "Spotify Streams Per User", height=5, ncol=1, open=TRUE, fouts=files.out)
