---
title: "M03 Lab — Data Visualization"
author: "Your Name"
date: today
format:
  html:
    toc: true
    toc-depth: 3
    theme: cosmo
    df-print: paged
    highlight-style: github
    embed-resources: true
execute:
  warning: false
  message: false
---

# Setup

Load the packages we'll use in this notebook. Two are new this week: `readxl` reads the Excel file you just created into R, and `scales` formats axis labels (it installs with the tidyverse but is not attached by `library(tidyverse)`, so it needs its own line).

```{r}
#| label: setup
#| warning: false
#| message: false

library(tidyverse)
library(here)
library(readxl)
library(scales)
```

# Introduction

*Replace this italicized paragraph with one paragraph of your own: what the data are (Pew Research Center, four social-media platforms, 2020-2025), where they came from, and the question your chart explores.*

# Data

The `here::here()` function builds a portable file path from the project root, so this line works regardless of where on your computer the project folder lives.

```{r}
#| label: import-data

social_media <- read_excel(here("data", "social_media.xlsx"))
```

A quick structural look at what you imported. You should see 24 rows and 3 columns.

```{r}
#| label: glimpse-data

social_media |> glimpse()
```

## Check your typing

`glimpse()` confirms the *shape* of your data. It cannot tell you whether you typed `5` where you meant `55`, paired a value with the wrong year, or spelled `TikTok` two different ways. A chart will render perfectly from wrong numbers — so check them before you plot.

```{r}
#| label: check-data

social_media |>
  summarize(
    rows = n(),
    platforms = n_distinct(platform),
    years = n_distinct(year),
    earliest_year = min(year),
    latest_year = max(year),
    percentages_in_range = all(percentage >= 0 & percentage <= 100),
    duplicate_platform_years = any(duplicated(pick(platform, year)))
  )
```

**Every one of these should match:** `rows` = 24, `platforms` = 4, `years` = 6, `earliest_year` = 2020, `latest_year` = 2025, `percentages_in_range` = `TRUE`, `duplicate_platform_years` = `FALSE`.

If `platforms` is 5, you have a spelling inconsistency (`Tiktok` and `TikTok` count as two). If `duplicate_platform_years` is `TRUE`, you typed the same platform-year twice and are missing another. Fix it in Excel, save, and re-run this chunk.

# Chart

You'll build the chart in four stages. **Run each chunk inline** (the green ▶) as you write it, and **render the whole document** (Cmd/Ctrl + Shift + K) after Stage 2, again after Stage 4, and once more after the Revised chart section is complete.

## Stage 1 — bare-bones line chart

```{r}
#| label: chart-stage1

# TODO: Build the bare-bones chart with year on x, percentage on y, and color by platform.
```

## Stage 2 — add points

```{r}
#| label: chart-stage2

# TODO (Stage 2): Take your Stage 1 chart and add points on top of the lines.
```

## Stage 3 — add labels (title-as-finding)

```{r}
#| label: chart-stage3
#| fig-width: 10
#| fig-height: 5

# TODO (Stage 3): Take your Stage 2 chart and add a labs() layer — axis titles,
# a legend title, a source caption, and a title that states your finding as a
# complete sentence. Write that title yourself; it is the point of the stage.
```

## Stage 4 — deliberate scales and a theme

```{r}
#| label: chart-stage4
#| fig-width: 10
#| fig-height: 5

# TODO (Stage 4): Take your Stage 3 chart and finish it with deliberate scales
# — a break for every year, a y-axis anchored at zero and labelled as percents
# — plus a theme. The lab page walks through each choice.
```

# Self-critique with Cairo's 5 qualities

Look at your Stage 4 chart and answer each of the five questions below — these are the same five on the lab page, Step 5. Replace each italicized *"Your answer:"* line with **one or two sentences** of your own response, about *your* chart.

**1. Truthful** — Does your title preserve the denominator (users *of that platform*)? Is your y-axis range defensible? What limitation is not visible in the figure?

*Your answer:*

**2. Functional** — Can a reader compare all four trajectories without bouncing back and forth to the legend?

*Your answer:*

**3. Beautiful** — Are the colors, line weights, and text size readable at the size this will actually be viewed?

*Your answer:*

**4. Insightful** — Name one thing your chart makes easy to see that the table of 24 numbers did not.

*Your answer:*

**5. Enlightening** — What should a careful reader conclude from your chart — and what should they *not* conclude?

*Your answer:*

Based on your self-critique above, identify ONE revision your chart needs.

## Revised chart

```{r}
#| label: chart-revised
#| fig-width: 10
#| fig-height: 5

# TODO: COPY your Stage 4 code here (leave Stage 4 in place so the before/after
# is visible), then make EXACTLY ONE deliberate revision based on your
# self-critique above. Re-render after the revision.
```

*What you changed and why (one or two sentences, linked to the Cairo quality that flagged the need):*
