# GOING OFF OF  DT.Acc.Using.perDPt

    ## Plot
    All_Pts <- unique(DT.Acc.Using.perDPt$Product_Tier)
    All_Pts <- setNames(nm=All_Pts)

createPTiers.Split <- function(y)  {
    ## Plot
    P.Usership.Tiers.SpotifyUsers <-
    lapply(All_Pts, function(.pt)  {
          y.ma <- sprintf("ma(%s, 3)", y)
          ggplot(data=DT.using.DPt[Product_Tier==.pt & Company=="Spotify"], aes(x=Month)) + 
            geom_bar(stat="identity", aes_string(y=y, fill="Product_Tier")) + 
            millions.y() + 
            scale_fill_manual(values=Colors) + 

            ## Moving Average
            geom_line(aes_string(y=y.ma), color="red") +

            # ## Average streams per user
            # geom_line(data=DT.Acc.Using.perD, aes(x=Month, y=Spotify_Streams_Per_User), color="Blue")  + 

            ## Revenue from fact_sales
        #    geom_line(data=qres.rev, aes(x=Month, y=net), color="dark blue") + 

            labs(x="Date", y=sprintf("Number of %s (in Millions)", y)
                , title=paste0("Orchard", "\nRed line is 3-month MA of monthly totals (in millions).") # \nBlue line is average number of streams per user (in units).")
                , fill="Product Tier")+ 

            facet_grid(Product_Tier~.) +
            nolegend()

        } )

    # printToPDF(P.Usership.Tiers.SpotifyUsers, "Spotify Usership by Tiers", main="Usership by Product Tier\n", sub=orchardFootNote())


    P.Usership.Tiers.OrchardUsers <-
    lapply(All_Pts, function(.pt)  {

        y.ma <- sprintf("ma(%s, 3)", y)
  

        if (.pt == "ThirdPartyPartners" && all(is.na(DT.using.DPt[Company=="Orchard" & Product_Tier==.pt][[y]]) )) 
            qplot(x=DT.using.DPt[Company=="Orchard"][ Users>0, list(unique(Month))][seq(1, length(V1), by=2), V1] , y=3e6, stat="identity", label="NA", geom="text") + 
                millions.y(limits=c(0 ,6e6)) + labs(y="", x="", title="ThirdPartyPartners\nNo Streaming Data Available")
        else


           ggplot(data=DT.using.DPt[Product_Tier==.pt & Company=="Orchard"], aes(x=Month)) + 
                geom_bar(stat="identity", aes_string(y=y, fill="Product_Tier")) + 
                millions.y() + 
                scale_fill_manual(values=Colors) + 

                ## Moving Average
                geom_line(aes_string(y=y.ma), color="red") +

                # ## Average streams per user
                # geom_line(data=DT.Acc.Using.perD, aes(x=Month, y=Spotify_Streams_Per_User), color="Blue")  + 

                ## Revenue from fact_sales
            #    geom_line(data=qres.rev, aes(x=Month, y=net), color="dark blue") + 

                labs(x="Date", y=sprintf("Number of %s (in Millions)", y)
                    , title=paste0("Spotify", "\nRed line is 3-month MA of monthly totals (in millions).\nBlue line is average number of streams per user (in units).")
                    , fill="Product Tier")+ 

                facet_grid(Product_Tier~.) +
                nolegend()

        } )

    printToPDF(c(P.Usership.Tiers.OrchardUsers, P.Usership.Tiers.SpotifyUsers)[as.vector(mapply(c, 1:4, 5:8))], sprintf("Spotify & Orchard %s by Tiers", y), main="Usership by Product Tier\nTHE ORCHARD\t\t\t\t\tSPOTIFY", sub=orchardFootNote(), ncol=2, byrow=FALSE)
    return(list(Orchard=P.Usership.Tiers.OrchardUsers, Spotify=P.Usership.Tiers.SpotifyUsers))
}

PPP <- createPTiers.Split("Users")

---------------------

DT.using.DPt[, PtAndSource := sprintf("%s (%s)", Product_Tier, Source)]

GOING OFF OF  DT.using.DPt WHICH INCLUDES "Source"

createPlotsByProduct_Tier <- function(y="Users") {

        lapply(All_Pts, function(.pt)  
        {
               y.ma <- sprintf("ma(%s, 3)", y)
               DT.using <- DT.using.DPt[Product_Tier==.pt & ! DT.using.DPt[Product_Tier==.pt, is.na(get(y)), by="Source,Company"]$V1 ]
               DT.using <- DT.using[!is.na(get(y))]
               DT.using[, Product_Tier := droplevels(Product_Tier)]
               DT.using[, Source := droplevels(Source)]
               DT.using[, Company := droplevels(Company)]
               # ggplot(data=DT.using.DPt[Product_Tier==.pt], aes(x=Month)) + 
               ggplot(data=DT.using, aes(x=Month)) + 
                    geom_bar(stat="identity", aes_string(y=y, fill="Product_Tier"), position=position_dodge()) + 
                    millions.y() + 
                    scale_fill_manual(values=Colors) + 

                    ## Moving Average
                    geom_line(aes_string(y=y.ma), color="red") +

                    # ## Average streams per user
                    # geom_line(data=DT.Acc.Using.perD, aes(x=Month, y=Spotify_Streams_Per_User), color="Blue")  + 

                    ## Revenue from fact_sales
                #    geom_line(data=qres.rev, aes(x=Month, y=net), color="dark blue") + 

                    labs(x="Date", y=sprintf("Number of %s (in Millions)", y)
                        , title=paste0(.pt, "\nRed line is 3-month MA of monthly totals (in millions).") # \nBlue line is average number of streams per user (in units).")
                        , fill="Product Tier")+ 

                    facet_grid(Product_Tier~Source+Company, scales="free", space="free") +
                    nolegend()
            } 
      )
    }

PP <- createPlotsByProduct_Tier()
PP[[1]]

printToPDF(PP, "Spotify & Orchard Reported vs Analytics", main="Usership by Product Tier\nTHE ORCHARD\t\t\t\t\tSPOTIFY", sub=orchardFootNote(), ncol=1, byrow=FALSE)


