Scatter Plot App

November 11, 2024

Overview

Idea for the App

Plan

  • Step 1: Wrangle data
  • Step 2: Code up a scatter plot with two variables
  • Step 3: Code UI with a title panel, sidebar with two dropdowns, and a main panel with the scatter plot output
  • Step 4: Code Server with reactive expressions to filter data and render the scatter plot
  • Step 5: Call the Shiny App (don’t forget!)

App Setup

Preliminaries

  • Start a new Shiny Web App
  • Name the app something like vdem-scatter-plot
  • Start a new R script in the folder that was created
    • Name the script something like wrangle.R
  • Use this file for the code in the next slide

Save the Data

library(dplyr)
library(readr)
library(vdemdata)

dem_data <- vdem |>
  filter(year == 2000) |>
  select(
    country = country_name, 
    polyarchy = v2x_polyarchy,
    clientelism = v2xnp_client,
    corruption = v2xnp_regcorr,
    womens_emp = v2x_gender,
    gdp_pc = e_gdppc,
    life_exp = e_pelifeex,
    education = e_peaveduc,
    region = e_regionpol_6C 
  ) |>   mutate(
    region = case_match(region, 
                        1 ~ "Eastern Europe", 
                        2 ~ "Latin America",  
                        3 ~ "Middle East",   
                        4 ~ "Africa", 
                        5 ~ "The West", 
                        6 ~ "Asia")
  )

# glimpse(dem_data)

write_csv(dem_data, "dem_data.csv")

Make the Setup Chunk

In the app.R file, use this code for the global section of the app…

# load packages
library(shiny)
library(readr)
library(ggplot2)

# load the data 
dem_data <- read_csv("dem_data.csv")

# create list of named values for the input selection
vars <- c("Democracy" = "polyarchy",
          "Clientelism" = "clientelism",
          "Corruption" = "corruption",
          "Women's Empowerment" = "womens_emp",
          "Wealth" = "gdp_pc",
          "Life Expectancy" = "life_exp", 
          "Education" = "education")

The User Interface (ui)

The UI Code

Copy and paste the UI code into the app.R file…

# Define UI for application that draws a scatter plot
ui <- fluidPage(

    # Application title
    titlePanel("Democracy and Development"),

    # Sidebar with a two dropdown menus
    sidebarLayout(
      sidebarPanel(
        selectInput(input = 'xcol', label = 'X Variable', choices = vars),
        selectInput(input = 'ycol', label = 'Y Variable', 
                    choices = vars, selected = vars[[6]])
      ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("scatterplot")
        )
    )
)

The UI Code

Adds the the title panel…

# Define UI for application that draws a scatter plot
ui <- fluidPage(

    # Application title
    titlePanel("Democracy and Development"),

    # Sidebar with a two dropdown menus
    sidebarLayout(
      sidebarPanel(
        selectInput(input = 'xcol', label = 'X Variable', choices = vars),
        selectInput(input = 'ycol', label = 'Y Variable', 
                    choices = vars, selected = vars[[6]])
      ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("scatterplot")
        )
    )
)

The UI Code

Defines the sidebar layout with the two dropdown panels…

# Define UI for application that draws a scatter plot
ui <- fluidPage(

    # Application title
    titlePanel("Democracy and Development"),

    # Sidebar with a two dropdown menus
    sidebarLayout(
      sidebarPanel(
        selectInput(input = 'xcol', label = 'X Variable', choices = vars),
        selectInput(input = 'ycol', label = 'Y Variable', 
                    choices = vars, selected = vars[[6]])
      ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("scatterplot")
        )
    )
)

The UI Code

Defines the main panel for the output. Note that we are calling plotOutput() and that we are calling the output “scatterplot” (the unique outputID for our plot)…

# Define UI for application that draws a scatter plot
ui <- fluidPage(

    # Application title
    titlePanel("Democracy and Development"),

    # Sidebar with a two dropdown menus
    sidebarLayout(
      sidebarPanel(
        selectInput(input = 'xcol', label = 'X Variable', choices = vars),
        selectInput(input = 'ycol', label = 'Y Variable', 
                    choices = vars, selected = vars[[6]])
      ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("scatterplot")
        )
    )
)

The Server (server)

The Server Code

Copy and paste the server code into the app.R file…

# Define server logic required to draw a scatter plot
server <- function(input, output, session) {
  
  # Render the plot
  output$scatterplot <- renderPlot({
    
    # ggplot call
    ggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +
      geom_point(aes(color = region)) +
      geom_smooth(method = "loess") +
      scale_color_viridis_d(option = "plasma") +
      theme_minimal() +
      labs(
        x =  names(vars[which(vars == input$xcol)]), # select names in vars that
        y =  names(vars[which(vars == input$ycol)]), # match input selections
        caption = "Source: V-Dem Institute",
        color = "Region"
      )
  })
}

The Server Code

Note that we are calling renderPlot(), which matches up with the plotOutput() in the UI. And we are referencing output as “scatterplot” (again, the unique outputID for our plot)…

# Define server logic required to draw a scatter plot
server <- function(input, output, session) {
  
  # Render the plot
  output$scatterplot <- renderPlot({
    
    # ggplot call
    ggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +
      geom_point(aes(color = region)) +
      geom_smooth(method = "loess") +
      scale_color_viridis_d(option = "plasma") +
      theme_minimal() +
      labs(
        x =  names(vars[which(vars == input$xcol)]), # select names in vars that
        y =  names(vars[which(vars == input$ycol)]), # match input selections
        caption = "Source: V-Dem Institute",
        color = "Region"
      )
  })
}

The Server Code

We dynamically reference the input values from the dropdown menus in the UI. We use input$xcol and input$ycol to reference the user-selected variables. As a result of tidy evaluation in ggplot2, these inputs have to be referenced in .data[[]] in order to work….

# Define server logic required to draw a scatter plot
server <- function(input, output, session) {
  
  # Render the plot
  output$scatterplot <- renderPlot({
    
    # ggplot call
    ggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +
      geom_point(aes(color = region)) +
      geom_smooth(method = "loess") +
      scale_color_viridis_d(option = "plasma") +
      theme_minimal() +
      labs(
        x =  names(vars[which(vars == input$xcol)]), # select names in vars that
        y =  names(vars[which(vars == input$ycol)]), # match input selections
        caption = "Source: V-Dem Institute",
        color = "Region"
      )
  })
}

The Server Code

Here we need to select the variable from the vector vars that created with the list of variables. We use which(vars == input$xcol) to find the index of the selected variable in the vector vars. We then use names() to get the name of the variable at that index…

# Define server logic required to draw a scatter plot
server <- function(input, output, session) {
  
  # Render the plot
  output$scatterplot <- renderPlot({
    
    # ggplot call
    ggplot(dem_data, aes(x = .data[[input$xcol]], y = .data[[input$ycol]])) +
      geom_point(aes(color = region)) +
      geom_smooth(method = "loess") +
      scale_color_viridis_d(option = "plasma") +
      theme_minimal() +
      labs(
        x =  names(vars[which(vars == input$xcol)]), # select names in vars that
        y =  names(vars[which(vars == input$ycol)]), # match input selections
        caption = "Source: V-Dem Institute",
        color = "Region"
      )
  })
}

Call the App

Run the App

To display the app, don’t forget this final line of code…


# Run the application 
shinyApp(ui = ui, server = server)

Your Turn!

  • Run the app and select different variables from the drop down menus.
  • What do you notice about the relationship between democracy and development?
  • How could you potentially improve the app?
10:00

Publish Your App

  • Install rsconnect:install.packages("rsconnect")
  • Create account and retrieve your API key
  • Configure RStudio:
library(rsconnect)
rsconnect::setAccountInfo(name='<account-name>', token='<account-token>', secret='<secret>')
  • Click the “Publish” button in RStudio toolbar and follow prompts
10:00