R Shiny app: Line charts for distinct sub-samples

Reading Time:

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.


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

ui <- fluidPage(
  titlePanel("Government spending and GDP growth"),
    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)
#    h2("main panel"),    
#    textOutput("text1"),

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"))
    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) {
  },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
  • re_w = Global growth rate of real government spending, weighted by 2015 GDP
  • gr_noChina = Same as #1, but excluding the Chinese economy.
  • re_noChina = Same as #2, but excluding China.
  • gr_advanced = Same as #1, but only limiting the sample to advanced economies only (as defined by the International Monetary Fund).
  • re_advanced = Same as #2, but calculated only for advanced economies.
  • gr_euro = Euro-area variation of #1.
  • re_euro = Euro-area variation of #2.

Global growth: How much slower without China? (rCharts post)

A simple growth chart coded with rCharts var chartParams = { "element": "globalGrowth", "width": 800, "height": 400, "xkey": "year", "ykeys": [ "gr_w", "gr_noChina" ], "data": [ { "year": "2005", "gr_...

Ukraine update - Q1 2016

Japan to allocate additional USD 300 mln loan for Ukraine in April | uatoday.tv FT: IMF warning sparks Ukraine pledge on corruption and reform Christine Lagarde,...