Lab 11
BUS 320
Open up your quarto project
- create a new quarto document in your posts folder
- save as lab11.qmd
- title: Lab 11
- format: dashboard
Dashboard
- 2 pages Data and Plots
Data page
- 1 row
# Data
## Interactive Table
Plots page
- 2 rows
# Plots
## Row {height=60%}
## Row {height=40%}
Content
- change code chunks from
{.r}to{r}
---
title: "Lab 11"
format: dashboard
---
### Set up r code chunk
```{.r}
#| include: false
# Load required packages and set global theme (chunk output hidden in render)
library(tidyverse) # ggplot2, dplyr, etc.
library(plotly) # interactive/animated plots
library(gapminder) # Gapminder dataset (install if needed)
library(scales) # number formatting helpers
library(DT) # interactive tables
theme_set(theme_minimal()) # set a minimal ggplot theme for all plots
```
# Data
## Interactive Table
```{.r}
datatable( # create an interactive table
gapminder, # use the gapminder dataset
rownames = FALSE, # hide row names
colnames = c("Country", "Continent", "Year", "Life Exp", "Population", "GDP per Capita") # set column labels
) |>
formatCurrency('gdpPercap', digits = 0) |> # show GDP per capita as currency, 0 decimals
formatRound(columns = c('lifeExp', 'pop'), digits = 0) # round life expectancy & population
```
# Plots
## Row {height=60%}
```{.r}
#| title: GDP and Life Expectancy
# Scatter of GDP vs Life Expectancy with animation by year
p <- gapminder |>
ggplot(
aes(
x = gdpPercap, # GDP per capita (x-axis)
y = lifeExp, # life expectancy (y-axis)
size = pop, # marker size encodes population
color = continent, # color by continent
frame = year, # animate frames over years
ids = country, # stable IDs for smooth animation
text = paste("Country:", country) # tooltip text
)
) +
geom_point(alpha = 0.7) + # semi-transparent points
scale_y_continuous(limits = c(25, 90)) +
scale_x_continuous(
labels = label_number(scale = 1e-3), # label x in thousands
limits = c(0, 50000)
) +
facet_wrap(~ continent, ncol = 5) + # small multiples by continent
labs(
x = "GDP per Capita $ (000's)",
y = "Life Expectancy",
color = NULL,
size = NULL
)
# Convert ggplot to interactive Plotly with animation controls
ggplotly(p) |>
animation_opts(frame = 500, transition = 300, redraw = FALSE) |> # timing
animation_slider(currentvalue = list(prefix = "Year: ")) # show current year
```
## Row {height=40%}
```{.r}
#| title: Population
# Stacked area chart of world population over time
p_pop <- gapminder |>
ggplot(aes(
x = year, # time on x-axis
y = pop, # population on y-axis
fill = continent, # fill by continent
group = country # group by country for stacking
)) +
geom_area(position = "stack") +
scale_y_continuous(
labels = label_comma(scale = 1e-9, suffix = "B") # show in billions
) +
labs(x = NULL, y = NULL, fill = NULL)
ggplotly(p_pop) # make interactive
```
```{.r}
#| title: Life Expectancy
# Lines of life expectancy over time by continent
p_life <- gapminder |>
ggplot(aes(
x = year, # year on x-axis
y = lifeExp, # life expectancy on y-axis
color = continent, # color lines by continent
group = country # group by country to draw separate lines
)) +
geom_line(alpha = 0.7) +
labs(x = NULL, y = NULL, color = NULL)
ggplotly(p_life) # make interactive
```
Render locally before publishing
- mac: cmd + shift + k
- pc: ctrl + shift + k
Stop local preview (rendering)
- Posit cloud: Stop any background jobs in the Background jobs tab
- Positron: Go to terminal Quarto preview (click on trash can)
Publish to Netlify
terminal quarto publish netlify
Check that your site on the web
- Submit the url to Assignment 11