November 19, 2026
faithful dataset in R (hint: ?faithful)eruptions columnvdem-scatter-plotwrangle.Rlibrary(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")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")ui)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")
)
)
)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")
)
)
)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")
)
)
)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")
)
)
)server)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"
)
})
}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"
)
})
}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"
)
})
}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"
)
})
}To display the app, don’t forget this final line of code…