Data and scope

The enrollment counts come from Tennessee Department of Education district profiles. The 2024–25 workbook identifies its totals as K–12 enrollment. Grade coverage and reporting practices may vary across earlier years. District records with missing counts are excluded from the rankings.

Choose a school year to see the five largest districts’ home counties on the map and the ten largest districts by reported enrollment in the bar chart. Two ranked districts may share a county. The map uses 2024 county boundaries for geographic reference, and the counts describe individual school districts.

Enrollment by school year

2024-25

District records with reported enrollment: 148 of 149.

Tennessee county map for 2024-25
District County Enrollment
Memphis-Shelby County Schools Shelby 105,856
Metro Nashville Public Schools Davidson 78,040
Knox County Knox 58,329
Rutherford County Rutherford 51,318
Hamilton County Hamilton 44,855
District enrollment ranking for 2024-25

Sources

Code

Data preparation

library(ggplot2)
library(sf)

csv <- path.expand(params$enrollment_csv)

if (!file.exists(csv)) {
  stop(
    "Run the enrollment extraction script first, ",
    "or update enrollment_csv in the report header.",
    call. = FALSE
  )
}

districts <- read.csv(csv, colClasses = c(district_id = "character"))

options(tigris_use_cache = TRUE)
counties <- tigris::counties(
  state = "TN",
  cb = TRUE,
  year = 2024,
  progress_bar = FALSE
)

fmt <- scales::label_comma()
years <- sort(unique(districts$year))
labels <- districts$school_year[match(years, districts$year)]

Map, bar chart, and school-year selector

# Match historical district names to their home counties.
locations <- c(
  "Memphis" = "Shelby",
  "Memphis - old" = "Shelby",
  "Shelby County" = "Shelby",
  "Shelby County - old" = "Shelby",
  "Shelby County Schools" = "Shelby",
  "Memphis-Shelby County Schools" = "Shelby",
  "Davidson County" = "Davidson",
  "Metro Nashville Public Schools" = "Davidson",
  "Knox County" = "Knox",
  "Knox County Schools" = "Knox",
  "Rutherford County" = "Rutherford",
  "Rutherford County Schools" = "Rutherford",
  "Hamilton County" = "Hamilton",
  "Hamilton County Schools" = "Hamilton"
)

tags <- htmltools::tags

# Embed each figure in the HTML report.
image_tag <- function(plot, height, alt) {
  path <- tempfile(fileext = ".png")
  on.exit(unlink(path))

  ggsave(
    filename = path,
    plot = plot,
    width = 8.5,
    height = height,
    dpi = 120,
    bg = "white"
  )

  tags$img(
    src = base64enc::dataURI(file = path, mime = "image/png"),
    alt = alt,
    style = "width:100%;height:auto"
  )
}

# Build the map, bar chart, and summary for each school year.
panels <- lapply(seq_along(years), function(i) {
  year <- years[i]
  records <- districts[districts$year == year, ]

  ranked <- records[is.finite(records$profile_enrollment), ]
  ranked <- ranked[order(-ranked$profile_enrollment, ranked$district), ]

  top_five <- head(ranked, 5)
  top_five$county <- unname(locations[top_five$district])

  stopifnot(
    nrow(ranked) >= 10L,
    all(top_five$county %in% counties$NAME)
  )

  # Map the home counties of the five largest districts.
  highlighted <- counties[counties$NAME %in% top_five$county, ]

  map <- ggplot() +
    geom_sf(
      data = counties,
      fill = "#e8eef1",
      color = "white",
      linewidth = 0.25
    ) +
    geom_sf(
      data = highlighted,
      fill = "#17657b",
      color = "white",
      linewidth = 0.3
    ) +
    coord_sf(crs = st_crs(5070), datum = NA) +
    labs(title = "Home counties of the five largest districts") +
    theme_void(base_size = 13)

  # Put the largest district at the top of the horizontal bar chart.
  top_ten <- head(ranked, 10)
  top_ten$label <- factor(top_ten$district, levels = rev(top_ten$district))

  bars <- ggplot(top_ten, aes(profile_enrollment, label)) +
    geom_col(fill = "#17657b") +
    geom_text(
      aes(label = fmt(profile_enrollment)),
      hjust = -0.12,
      size = 3.3
    ) +
    scale_x_continuous(
      labels = fmt,
      expand = expansion(mult = c(0, 0.19))
    ) +
    labs(
      title = "Ten largest districts by reported enrollment",
      x = "Students enrolled",
      y = NULL
    ) +
    theme_minimal(base_size = 12) +
    theme(panel.grid.major.y = element_blank())

  key <- data.frame(
    District = top_five$district,
    County = top_five$county,
    Enrollment = fmt(top_five$profile_enrollment)
  )

  tags$section(
    id = paste0("year-", year),
    class = "enrollment-panel",
    hidden = if (i != length(years)) "hidden",
    tags$h3(labels[i]),
    tags$p(sprintf(
      "District records with reported enrollment: %d of %d.",
      nrow(ranked),
      nrow(records)
    )),
    image_tag(map, 3.2, paste("Tennessee county map for", labels[i])),
    htmltools::HTML(as.character(knitr::kable(key, format = "html"))),
    image_tag(bars, 5.4, paste("District enrollment ranking for", labels[i]))
  )
})

# Select the latest school year when the report opens.
picker <- tags$select(
  id = "school-year",
  lapply(seq_along(years), function(i) {
    tags$option(
      value = years[i],
      selected = if (i == length(years)) "selected",
      labels[i]
    )
  })
)

cat(as.character(htmltools::tagList(
  tags$style(".enrollment-panel[hidden] { display: none !important; }"),
  tags$p(tags$label(`for` = "school-year", "School year "), picker),
  panels,
  tags$script(htmltools::HTML("
    (function () {
      var picker = document.getElementById('school-year');

      function updateYear() {
        document.querySelectorAll('.enrollment-panel').forEach(function(panel) {
          panel.hidden = panel.id !== 'year-' + picker.value;
        });
      }

      picker.addEventListener('change', updateYear);
      updateYear();
    }());
  "))
)))