R Shiny app: Line charts for distinct sub-samples

Tutorial: reproducing a fiscal chart

A few days ago, I posted a chart that looked like this [static screenshot below]


The dataset is posted at: https://github.com/zilinskyjan/datasets/blob/master/fiscal/fiscal_global.csv

To reproduce or experiment with the chart, you'll need to: create an app folder; Set the appropriate working directory using setwd("..."); Familiarize yourself with the variables (labels explained below).

All lines below should go to a single compact app.R file, rather than using ui.R with server.R.

library(shiny)

dat <- read.csv("fiscal_global.csv")  
year <- dat$year

ui <- fluidPage(

  titlePanel("Government spending and GDP growth"),

  sidebarLayout(
    sidebarPanel(

    radioButtons("radio", label = h3("Select the sample of countries"),
                 choices = list("World", "All countries except China",
                                "Advanced Economies only", "Eurozone"),selected = "World"),

    selectInput("vertical","Show vertical line in year(s):", 
                choices = unique(dat$year),multiple=TRUE
                ),

    checkboxInput("hor", "Show horizontal axis", TRUE)

    ),

    mainPanel(
#    h2("main panel"),    
#    textOutput("text1"),
    plotOutput("lineChart")
    )   
  )      
)

server <- function(input, output) {

#  output$text1 <- renderText({ 
#    paste("The chart is showing fiscal stimulus and real economy growthYou have selected ", input$radio)
#  })

  output$lineChart <- renderPlot({  
    chartData <- switch(input$radio,
                        "World" = list(dat$gr_w,dat$re_w),
                        "All countries except China" = list(dat$gr_noChina,dat$re_noChina),
                        "Advanced Economies only" = list(dat$gr_advanced,dat$re_advanced),
                        "Eurozone" = list(dat$gr_euro,dat$re_euro) 
    )  

    chartTitle <- switch(input$radio,
                        "World" = "the world",
                        "All countries except China" = "all countries except China",
                        "Advanced Economies only" = "the advanced economies",
                        "Eurozone" = "the euro area" 
    )

    yrange <- c(-4,12)
    xrange <- range(year)
    plot(xrange,yrange,type="n",xlab="",ylab="Growth rate (percent)",cex.lab=1.5,
         main=paste("GDP-weighted averages shown for", chartTitle),
         sub=c("Data: IMF WEO (10/2015). Chart J. Zilinsky \n Note: Data for 2016 are IMF projections"))
    lines(year,chartData[[1]],col="aquamarine4",lwd=3)
    lines(year[2:12],na.omit(chartData[[2]]),col="firebrick3",lwd=3)
    abline(v=input$vertical,lty=2) 
    legend(2012,8,c("Real government spending","Real GDP"), 
           col=c('firebrick3','aquamarine4'),pch=15,ncol=1,bty ="n",cex=1.1)

     if (input$hor) {
        abline(h=0)  
     } 
  },height = 500, width = 600)

}

shinyApp(ui = ui, server = server)  

Variables in fiscal_global.csv:

  1. gr_w = Global growth rate of real GDP, weighted by 2015 GDP
  2. re_w = Global growth rate of real government spending, weighted by 2015 GDP
  3. gr_noChina = Same as #1, but excluding the Chinese economy.
  4. re_noChina = Same as #2, but excluding China.
  5. gr_advanced = Same as #1, but only limiting the sample to advanced economies only (as defined by the International Monetary Fund).
  6. re_advanced = Same as #2, but calculated only for advanced economies.
  7. gr_euro = Euro-area variation of #1.
  8. re_euro = Euro-area variation of #2.