# Recreates Farlight’s front-page comparison and social preview using base R.
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1L) stop("Provide the path to journal-front-page.csv.")
data <- read.csv(args[[1]], check.names = FALSE)
out_dir <- if (length(args) >= 2L) args[[2]] else dirname(args[[1]])
social <- if (length(args) >= 3L) args[[3]] else file.path(out_dir, "ownership-social.png")
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)

before_color <- "#9eb3c4"
after_color <- "#245574"
paper <- "#f8f7f3"
navy <- "#142b40"
ink <- "#14283a"
mint <- "#b8eadd"

png(file.path(out_dir,"journal-front-page.png"), width = 1200, height = 660, res = 120, type = "cairo", bg = paper)
par(mar = c(3.5, 1, 1, 1), family = "sans", xaxs = "i", yaxs = "i")
plot.new()
plot.window(xlim = c(0, 43), ylim = c(0, 8.8))
for (x in seq(0, 30, 10)) segments(0 + x, .3, 0 + x, 6.65, col = "#d3dde4", lwd = 1.3)
text(0, 8.35, "Where the Journal put its front-page space", adj=c(0,.5), font=2, cex=1.8, col=ink)
rect(0, 7.33, .9, 7.62, col=before_color, border=NA)
text(1.25, 7.48, "Before acquisition", adj=c(0,.5), col=ink, cex=.95)
rect(11, 7.33, 11.9, 7.62, col=after_color, border=NA)
text(12.25, 7.48, "After acquisition", adj=c(0,.5), col=ink, cex=.95)
text(43, 7.48, "Change", adj=c(1,.5), font=2, col=ink)
for (i in seq_len(nrow(data))) {
  y <- 6.3 - (i - 1) * 2.05
  before <- data$before_percent[[i]]
  after <- data$after_percent[[i]]
  change <- after - before
  text(0, y, data$topic[[i]], adj=c(0,.5), col=ink, font=2, cex=1.02)
  rect(0, y-.56, before, y-.32, col=before_color, border=NA)
  rect(0, y-1.05, after, y-.80, col=after_color, border=NA)
  text(before+.45, y-.44, paste0(before,"%"), adj=c(0,.5), col=ink, cex=.92)
  text(after+.45, y-.91, paste0(after,"%"), adj=c(0,.5), col=ink, cex=.92)
  text(43, y-.62, sprintf("%+d points",change), adj=c(1,.5), col=ink, font=2, cex=1.06)
}
axis(1, at=seq(0,30,10), labels=paste0(seq(0,30,10),"%"), tck=-.018, cex.axis=.85, col.axis="#354d61", line=0)
mtext("Share of sampled front-page space. Source: Pew Research Center, April 2008.", side=1, line=2.2, adj=0, cex=.79, col="#354d61")
dev.off()

png(social,width=1200,height=630,res=120,type="cairo",bg=navy)
par(mar=rep(0,4), xaxs="i", yaxs="i", family="sans")
plot.new()
plot.window(xlim=c(0,1200),ylim=c(0,630))
rect(0,614,1200,630,col=mint,border=NA)
text(72,550,"FARLIGHT NEWS  /  INVESTIGATION",adj=c(0,.5),col=mint,font=2,cex=1.28)
text(72,411,"Who owns",adj=c(0,.5),col="#fffefa",family="serif",font=2,cex=5.2)
text(72,293,"the news?",adj=c(0,.5),col="#fffefa",family="serif",font=2,cex=5.2)
segments(700,174,700,515,col="#5e7c92",lwd=2)
text(754,403,"$47B",adj=c(0,.5),col=mint,font=2,cex=3.65)
text(754,323,"in new equity behind the deal",adj=c(0,.5),cex=1.16,col="#eaf2f7")
text(754,285,"that brought CNN and CBS News",adj=c(0,.5),cex=1.16,col="#eaf2f7")
text(754,247,"under one parent",adj=c(0,.5),cex=1.16,col="#eaf2f7")
text(72,108,"What changed when the money moved",adj=c(0,.5),col="#eaf2f7",cex=1.45)
text(1128,108,"farlightnews.com",adj=c(1,.5),col=mint,cex=1.22)
dev.off()
