sample-interactive

Khue Tran

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Load in the libraries

Show code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Show code
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
Show code
library(here)
here() starts at /Users/khuetran/_Github/khuetran/quarto
Show code
theme_set(theme_minimal())

Tuberculosis Data

First view at the data

Show code
tb_aus <- read_csv(here("data", "TB2020.csv")) |>
  filter(country == "Australia")
Rows: 40800 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): country, age_group, sex
dbl (2): year, count

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Show code
p <- tb_aus |>
  group_by(sex, age_group) |>
  summarise(count = sum(count)) |>
  ggplot(aes(sex, count, fill = age_group)) +
    geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Sex", y = "Count", fill = "Age Group", title = "Tuberculosis cases reported in Australia from 1997-2012")
`summarise()` has grouped output by 'sex'. You can override using the `.groups`
argument.
Show code
ggplotly(p)

Some more tweaks for more insights

Show code
library(crosstalk)
tb_A <- read_csv(here("data", "TB2020.csv")) |>
  filter(startsWith(country, "A"))
Rows: 40800 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): country, age_group, sex
dbl (2): year, count

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Show code
d <- SharedData$new(tb_A)

bscols(widths = c(2, NA),
       list(
         filter_checkbox("sex", "Sex", d, ~sex, inline = TRUE),
         filter_checkbox("age_group", "Age Group", d, ~age_group),
         filter_slider("year", "Year", d, ~year, width = "100%"),
         filter_select("country", "Country", d, ~country, multiple = FALSE)
       ),
       plot_ly(d, x = ~year, y = ~count, color = ~age_group,
               symbol = ~sex, type = "scatter",
               mode = "lines+markers", marker = list(size = 10)))