## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>",
                      fig.width = 7, fig.height = 4)

## ----setup--------------------------------------------------------------------
library(steves)
library(dplyr)
library(ggplot2)

## -----------------------------------------------------------------------------
glimpse(episodes)

## -----------------------------------------------------------------------------
episodes |>
  count(primary_country, flag, sort = TRUE) |>
  head(10)

## ----country-bar, fig.height = 5, fig.alt = "Horizontal bar chart of the number of Rick Steves' Europe episodes set in each country, sorted from most to fewest."----
episodes |>
  count(primary_country) |>
  filter(primary_country != "Multiple") |>
  mutate(primary_country = forcats::fct_reorder(primary_country, n)) |>
  ggplot(aes(n, primary_country)) +
  geom_col(fill = "#1B3A6B") +
  labs(title = "Episodes per country",
       x = "Episodes", y = NULL) +
  theme_minimal()

## -----------------------------------------------------------------------------
episodes |>
  filter(!is.na(lat)) |>
  group_by(region) |>
  summarise(n = n(),
            median_rating = median(imdb_rating_shrunk)) |>
  arrange(desc(median_rating))

## ----leaflet, eval = requireNamespace("leaflet", quietly = TRUE)--------------
library(leaflet)

episodes |>
  filter(!is.na(lat)) |>
  leaflet() |>
  addTiles() |>
  addCircleMarkers(
    ~long, ~lat,
    radius = ~ pmax(3, imdb_rating_shrunk - 5),
    popup  = ~ sprintf("<b>%s</b><br>%s %s<br>%s",
                       title, flag, primary_country, best_summary),
    color = "#1B3A6B", fillOpacity = 0.6, stroke = FALSE
  )

## ----cadence, fig.alt = "Bar chart of episodes aired per calendar year, showing seasonal production cadence from 2000 to 2025."----
episodes |>
  count(air_year) |>
  ggplot(aes(air_year, n)) +
  geom_col(fill = "#FFC72C") +
  labs(title = "Episodes aired per year",
       x = NULL, y = "Episodes") +
  theme_minimal()

