App: World Development Indicators

Stefano Sanfilippo
2021/07/03

The data

Data are from GAPMINDER: https://gapminder.org.

It's a dataset of 1704 rows and 6 variables:

  • country: the data set holds 146 countries.
  • continent.
  • year: from 1952 to 2007, at intervals of 5.
  • lifeExp: life expectancy. It is the average numbers of years a new born child would live if current mortality patterns were to stay the same.
  • pop: Population living in a country.
  • gdpPercap: GDP per Capita. It is the annual Gross Domestic Product, divided for the population. We plot on the X-Axis.

Layout

plot of chunk unnamed-chunk-1

Description (1)

The app is composed by a plot on the right and a panel and a sidebar panel on the left.

Sidebar panel

In the sidebar panel we have two dropdowns which allow to select the data to be displayed in the plot:

  1. Year: it allows to choose the year for displaying the data. The years are in intervals of 5 (because of the original data). The default option is 2007, the last year in the dataframe.
  2. Continent: this dropdown allows to filter the data by continent. The default option is All.

Description (2)

Plot

The plot is a bubble plot. Every observation -corresponding to a country- is a bubble, the position of which in the XY square is fixed by two values:

  • On the X-axis we put the GDP/capita.
  • On the Y-axis we put the Life expectancy.
  • On the other hand the size of the bubble is in relation to the population.

Every bubble can display a label hovering the mouse pointer on it. The label include 4 values:

  • Name of the country
  • Life expectancy of the country
  • GDP/capita of the country
  • Population of the country

The shiny Code

This is a shiny app that uses plotly to build its graphics.

ui

  • The UI code entails 2 selectInputs for “year” and “Continent”.
  • The main panel has got one plot output.
  • We added some explanation text.

server

  • Note that we have 2 options of filtering the dataframe, the first (if(input$Continent == “All”)) is in order to have the option to view all the countries unfiltered.
library(shiny)
ui <- shinyUI(fluidPage(
    titlePanel("World development indicators (1952 - 2007)"),  # Add a title panel
    sidebarLayout(  # Make the layout a sidebarLayout
        sidebarPanel(
            selectInput(inputId = "year",
                         label = "Year",
                         choice =  c(2007,2002,1997,1992,1987,1982,1977,
                                     1972,1967,1962,1957,1952),
                         selected = 2007),
            h5("This dropdown allows you to choose the year for displaying the data. The years are in intervals of 5. The default option is 2007."),
            selectInput(inputId = "Continent",
                        label = "Continent",
                        choice = c("Africa", "Americas", "Asia", "Europe", "Oceania", "All"),
                        selected = "All"),
            h5("This dropdown allows you to filter the data by continent. The default option is 'All'.")                     ...
...
        ),
        mainPanel(plotlyOutput("plot1"),
                  h5("1. The plot is a bubble plot. Every observation -corresponding to a country- is a bubble, the position of which in the XY square is fixed by 'GDP/capita' and 'Life expectancy. The size of the bubble is proportional to the population."),
                  h5("2. Every bubble can display a label hovering the mouse pointer on it. The label include 4 values: 1) Name of the country; 2) Life expectancy of the country; 3) GDP/capita of the country; 4) Population of the country."))
library(shiny)  
library(tidyverse)  
library(plotly)
library(gapminder)

server <- shinyServer(function(input, output) {
    output$plot1 <- renderPlotly({
        if(input$Continent == "All") {
            Data <- gapminder %>%
                filter(year == input$year) 
        } else {Data <- gapminder %>%
            filter(year == input$year & continent==input$Continent) 
        }

        x <- list(title = "GDP/capita")
        y <- list(title = "Life expectancy")
        p <- plot_ly(Data, x= ~gdpPercap, y= ~lifeExp, mode="markers",
                     color = ~country, size= ~pop,
                     hoverinfo = 'text',                    ...
...                text =  ~paste(country,'</br></br>',
                                "Life expectancy:", round(lifeExp,1),'</br>',
                                "GDP/Capita:", round(gdpPercap,0),'</br>',
                                "Population:", format(pop, big.mark=","))) %>% 
        layout(title = paste("World: GDP/capita, life expectancy and population
                             year in the year",
                                 input$year,"- Continent:", input$Continent),
            xaxis = x, yaxis = y,
            annotations = list(x = 0, y = -0.1, text = "Source: GAPMINDER.ORG", 
                            showarrow = F, xref='paper', yref='paper', 
                            xanchor='left', yanchor='auto', xshift=0, yshift=0,
                            font=list(size=15, color="blue"))) %>% 
            hide_legend()
    }) })