library(tidyverse)
library(ggplot2)
library(ggthemes)
library(socviz)
library(maps)
library(mapproj)
library(questionr)
library(viridis)
library(leaflet)
library(tidycensus)
library(knitr)
library(dplyr)
library(readxl)DAT-4313 - Lab 7.1
School Data
GET KANSAS POPULATION DATA BY COUNTY DIRECTLY FROM THE US CENSUS VIA API
census_api_key("4e54c9226f6c18a4ef1873e3fa856936b9fa1775") #use this one when rendering
ks_pop <-
get_acs(geography = "county",
variables = "B01003_001",
state = "KS",
geometry = TRUE)head(as_tibble(ks_pop))# A tibble: 6 × 6
GEOID NAME variable estimate moe geometry
<chr> <chr> <chr> <dbl> <dbl> <MULTIPOLYGON [°]>
1 20175 Seward County, Kansas B01003_… 21942 NA (((-101.0681 37.38778, -…
2 20027 Clay County, Kansas B01003_… 8088 NA (((-97.3707 39.21899, -9…
3 20171 Scott County, Kansas B01003_… 5128 NA (((-101.1284 38.7006, -1…
4 20047 Edwards County, Kansas B01003_… 2849 NA (((-99.56988 37.91302, -…
5 20147 Phillips County, Kans… B01003_… 4960 NA (((-99.62821 39.72017, -…
6 20149 Pottawatomie County, … B01003_… 25482 NA (((-96.72774 39.42685, -…
CHOLOPLETH FOR KANSAS COUNTIES
MapPalette <- colorQuantile(palette = "viridis", domain = ks_pop$estimate, n = 20)
library(sf)
ks_pop %>%
st_transform(crs = "+proj=longlat +datum=WGS84") %>%
leaflet(width = "100%", height = 500) %>%
addProviderTiles(provider = "Esri.WorldPhysical") %>%
addPolygons(popup = ~NAME,
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Population Percentiles",
opacity = 1) Kansas is a diverse state in terms of population. Western Kansas is known for having very few people, while Sedgwick County and Johnson County hold a high percentage of the population. The two major Universities in Kansas, Kansas State University and Kansas University reside in Riley County and Johnson County.
SCHOOL LOCATIONS IN KANSAS
Public_Schools <- read.csv("Public_Schools.csv")
Private_Schools <- read.csv("Private_Schools.csv")
public_schools_ks <- Public_Schools %>%
filter(STATE == "KS")
private_schools_ks <- Private_Schools %>%
filter(STATE == "KS")
# Public Schools
public_schools_ks %>% leaflet(width = "100%") %>%
addTiles() %>%
setView(-98.0, 39.0, zoom = 6) %>%
addMarkers(lat = ~LATITUDE,
lng = ~LONGITUDE,
popup = ~NAME)# Private Schools
private_schools_ks %>% leaflet(width = "100%") %>%
addTiles() %>%
setView(-98.0, 39.0, zoom = 6) %>%
addMarkers(lat = ~LATITUDE,
lng = ~LONGITUDE,
popup = ~NAME)There are more public schools than private schools, but this is because public schools are split up into multiple schools depending on grade level. Both public and private hold a similar trend as they both seem to cluster a lot in Sedgwick County and Johnson County. As I mentioned previously, these counties hold the highest densities.
Aquired Data from Kaggle (US Schools Dataset - LARXEL).
SCHOOL LOCATIONS OVERLAYED ONTO THE CHOROPLETH
# Used ChatGPT
library(leaflet)
library(sf)
public_schools_ks$type <- "Public"
private_schools_ks$type <- "Private"
public_schools_ks <- public_schools_ks[, c("NAME", "LONGITUDE", "LATITUDE", "ENROLLMENT", "type")]
private_schools_ks <- private_schools_ks[, c("NAME", "LONGITUDE", "LATITUDE", "ENROLLMENT", "type")]
combined_schools <- bind_rows(public_schools_ks, private_schools_ks)
sorted_schools <- combined_schools[order(-combined_schools$ENROLLMENT), ]
class_sizes <- c(16, 16, 32, 48, 48, 48, Inf)
class_names <- c("7", "6", "5", "4", "3", "2", "1")
sorted_schools$class <- cut(x = rank(-sorted_schools$ENROLLMENT, ties.method = "first"),
breaks = cumsum(c(0, class_sizes)),
labels = class_names,
include.lowest = TRUE)I read online that schools were split up into the 7 classification based on which schools had the highest number of students. Using the enrollment variable, I was able to subset the schools into the 7 classifications. While it is not perfect, it does the job for collecting some valuable insights.
# Used ChatGPT
MapPalette <- colorQuantile(palette = "viridis", domain = ks_pop$estimate, n = 20)
ks_pop %>%
st_transform(crs = "+proj=longlat +datum=WGS84") %>%
leaflet(width = "100%", height = 500) %>%
addProviderTiles(provider = "Esri.WorldPhysical") %>%
addPolygons(popup = ~NAME,
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ MapPalette(estimate)) %>%
addLegend("bottomright",
pal = MapPalette,
values = ~ estimate,
title = "Population Percentiles",
opacity = 1) %>%
addCircleMarkers(data = sorted_schools,
lng = ~LONGITUDE,
lat = ~LATITUDE,
radius = ~class,
color = ~type,
fillColor = ~colorFactor(palette = c("red", "blue"), domain = c("Public", "Private"))(type),
fillOpacity = 0.7,
popup = ~NAME) %>%
addLegend("bottomleft",
pal = colorFactor(palette = c("red", "blue"), domain = c("Public", "Private")),
values = sorted_schools$type,
title = "School Type",
opacity = 1)I decided to separate the type of school (private or public) by the color. Red means that the school is private while blue means that it is public. I also made the size of the circle the classification of the school (1A-7A). The bigger the circle, the higher the classification, and the larger the student body. By doing this, I was able to create some insights. The biggest schools are in the counties with the highest populations. While there are not many big private schools, there seems to be a great deal of them clustered up in the high population counties.