R/eco.plot.R

Defines functions eco.plot

Documented in eco.plot

eco.plot <-function(plot_type, df,x,y1,y2,y3,ycolor1,ycolor2,ycolor3,subtitle,y.title,x.title,title,caption,legend.pos,date.break, date.format,line.size,yaxis.text.size,xaxis.text.size,title.size,caption.size,legend.size, axis.title.size, bar.position,fill){

  # Set Default Values
  plot_type <-ifelse(missing(plot_type),'line',tolower(plot_type))
  x <- ifelse(missing(x),'date',x)
  subtitle <- ifelse(missing(subtitle),'',subtitle)
  y.title <- ifelse(missing(y.title),'', y.title)
  x.title <- ifelse(missing(x.title),'', x.title)
  title <- ifelse(missing(title),'', title)
  caption <- ifelse(missing(caption),'', caption)
  legend.pos <- ifelse(missing(legend.pos),'top', legend.pos)
  ycolor1 <- ifelse(missing(ycolor1),'#014d64',ycolor1)
  ycolor2 <-ifelse(missing(ycolor2),'#00887d',ycolor2)
  ycolor3 <-ifelse(missing(ycolor3),'#90353B',ycolor3)
  date.break <- ifelse(missing(date.break),'1 year', date.break)
  date.format <- ifelse(missing(date.format),'%y', date.format)
  line.size <-ifelse(missing(line.size),.7, line.size)
  yaxis.text.size <-ifelse(missing(yaxis.text.size),15, yaxis.text.size)
  xaxis.text.size <-ifelse(missing(xaxis.text.size),15, xaxis.text.size)
  caption.size <-ifelse(missing(caption.size),13, caption.size)
  legend.size <-ifelse(missing(legend.size),15, legend.size)
  title.size <-ifelse(missing(title.size),20, title.size)
  axis.title.size <-ifelse(missing(axis.title.size), 15, axis.title.size)
  bar.position <- ifelse(missing(bar.position),'dodge',bar.position) # GGPLOT 2 Options ie(dodge, stack,fill)
  fill <-ifelse(missing(fill), 'NA', fill)

  # Plot Template function used to build different variations of graphs
  base_plot <-ggplot()+
    labs(subtitle=subtitle, y=y.title,x=x.title,title=title,caption=caption)+
    theme_economist_white(gray_bg = FALSE)+
    theme(
      legend.position = legend.pos,
      legend.title = element_blank(),
      axis.text.x = element_text(size=xaxis.text.size),
      axis.text.y = element_text(size=yaxis.text.size),
      axis.title = element_text(size=axis.title.size),
      plot.caption = element_text(size=caption.size),
      legend.text = element_text(size=legend.size),
      plot.title = element_text(size= title.size)
    )

  # Defines plot variations
  # Plots single line on grid
  if(plot_type == 'line'){
    # Plots single line on grid
    eco_plot<- base_plot +
      geom_line(aes(x=df[[x]], y=df[[y1]],color=ycolor1),size=line.size) +
      scale_color_manual(labels=c(y1),values=c(ycolor1))

  } else if (plot_type == 'dual line') {
    # Plots two lines on single grid
    eco_plot<-base_plot +
      geom_line(aes(x = df[[x]], y = df[[y1]], color = ycolor1),size=line.size) +
      geom_line(aes(x = df[[x]], y=df[[y2]],color = ycolor2),size =line.size) +
      scale_color_manual(values = c(ycolor1, ycolor2),labels = c(y1,y2))
  } else if (plot_type == 'tri line'){
    # Plots three lines on single grid
    eco_plot <-base_plot +
      geom_line(aes(x = df[[x]], y = df[[y1]], color = ycolor1),size=line.size)+
      geom_line(aes(x = df[[x]], y=df[[y2]],color = ycolor2),size =line.size)+
      geom_line(aes(x = df[[x]], y=df[[y3]],color = ycolor3),size =line.size)+
      scale_color_manual(values = c(ycolor1, ycolor2,ycolor3),labels = c(y1,y2,y3))
  } else if (plot_type == 'bar'){
    # Plots bar
    if (fill == 'NA'){
      eco_plot <-base_plot +
        geom_bar(aes(x=df[[x]], y=df[[y1]]),stat='identity',position = bar.position)
    } else{
      # With Fill
      eco_plot <-base_plot +
        geom_bar(aes(x=df[[x]], y=df[[y1]], fill=df[[fill]]),stat='identity',position = bar.position)

    }

  } else if (plot_type == 'bar line'){
    eco_plot <-base_plot +
      geom_bar(aes(x=df[[x]], y=df[[y1]], fill=df[[fill]]),stat='identity',position = bar.position) +
      geom_line(aes(x = df[[x]], y = df[[y2]]),size=line.size) +
      scale_color_manual(labels=c(y2),values=c(ycolor2))

  }

  if(inherits(x, 'Date')==TRUE){
    eco_plot +
      scale_x_date(date_breaks = date.break,labels = date_format(date.format))
  }

  # Adds recession bars to chart
  if("recession" %in% colnames(df)){

    if(head(df[['recession']],1)== 1){
      start <-df$date[head(df[['recession']],1)]
    } else {
      start <-df$date[which(diff(df[['recession']])==1)]
    }

    end <-df$date[which(diff(df[['recession']])==-1)]

    if (length(end)>length(start)){
      end <-end[-1]
      recession.df <-data.frame(start = start, end = end)
      #recession.df <- subset(recession.df, start>=min(df[['recession']]))
    } else {
      recession.df <-data.frame(start = start, end = end)
      #recession.df <- subset(recession.df, start>=min(df[['recession']]))
    }

    eco_plot <-eco_plot +
      geom_rect(data=recession.df,aes(xmin=start,xmax=end, ymin=-Inf,ymax=+Inf),alpha=.3,color='grey80')
    return(eco_plot)

  } else {
    return(eco_plot)
  }

}
soloemoon/ecoMoon documentation built on Oct. 7, 2022, 9:59 a.m.