require(ggplot2)

agg = c("CTR", "CPA")
clr = "Age"

GenderSummary = TRUE
AgeSummary    = TRUE 

trim = TRUE
sd.thresh.for.trim = 3

# Filter by category
datUse <- dat[] [!is.na(value)]
datUse[, .using := TRUE]

# -------------------------------------------- #
## Do not use TOTAL columns and the other values
# -------------------------------------------- #
  setkeyIfNot(datUse, "Gender")
  if (GenderSummary) {
    datUse[!.("TOTAL"), .using := FALSE]
  } else 
    datUse[ .("TOTAL"), .using := FALSE]

  setkeyIfNot(datUse, "Age")
  if (AgeSummary) {
    datUse[!.("TOTAL"), .using := FALSE]
  } else 
    datUse[ .("TOTAL"), .using := FALSE]
# -------------------------------------------- #

# Flag as not being used, any row with an extreme value
if (trim)
  datUse[as.vector(abs(scale(value)) > sd.thresh.for.trim * sd(value))
         , .using := FALSE, by=variable]


## Set the key, for easy filtering
setkey(datUse, variable, .using)
setkey(fb.Total, variable)


### COMPARE AGE GROUPS
G <- 
ggplot(data=datUse[.(agg, TRUE)], aes(x=Date, y=value, color=eval(parse(text=clr)))) + 
  geom_point(size=1) + geom_line(size=0.2)


  
G + 

GT + G

geom_point(data=fb.Total[.(agg)], aes(x=Date, y=value), size=2, color="red") + 
      geom_line(data=fb.Total[.(agg)], aes(x=Date, y=value), size=1.2, alpha=0.5) + 
      facet_grid(variable~.) + G

