library(leaflet)
library(sf)
library(tidyverse)
library(dplyr)
#read colleges and universities data
(colleges = read_csv('Colleges_Universities.csv') %>%
#filter to just colleges and junior colleges
filter(NAICS_DESC %in% c("COLLEGES, UNIVERSITIES, AND PROFESSIONAL SCHOOLS", "JUNIOR COLLEGES")) %>%
#filter to just the USA
filter(COUNTRY %in% c("USA")) %>%
st_as_sf(coords = c("LONGITUDE", "LATITUDE"), crs = 4326))
## Simple feature collection with 4545 features and 42 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -159.396 ymin: 19.70185 xmax: -67.24331 ymax: 71.3247
## Geodetic CRS: WGS 84
## # A tibble: 4,545 × 43
## OBJECTID IPEDSID NAME ADDRESS CITY STATE ZIP ZIP4 TELEPHONE TYPE STATUS
## * <dbl> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <chr>
## 1 7002 182148 ALTI… 170 NO… HEND… NV 89074 8811 (702) 56… 2 A
## 2 7003 189547 SUNY… 907 UP… BING… NY 13905 NOT … (607) 77… 1 A
## 3 7005 190442 CORN… 1 ACAD… CORN… NY 14830 NOT … (607) 96… 1 A
## 4 7006 193070 MESI… 145 E … NEW … NY 10002 6301 (212) 96… 2 A
## 5 7007 193399 COLL… 6301 R… BRONX NY 10471 1093 (718) 40… 2 A
## 6 7008 193751 NEW … 2360 S… SENE… NY 13148 0800 (315) 56… 2 A
## 7 7010 195216 ST L… 23 ROM… CANT… NY 13617 NOT … (315) 22… 2 A
## 8 7011 195562 ST. … 155 W … PATC… NY 11772 NOT … (631) 68… 2 R
## 9 7012 196130 SUNY… 1300 E… BUFF… NY 14222 NOT … (716) 87… 1 A
## 10 7013 200590 ETI … 2076 Y… NILES OH 44446 4398 (330) 65… 3 A
## # ℹ 4,535 more rows
## # ℹ 32 more variables: POPULATION <dbl>, COUNTY <chr>, COUNTYFIPS <chr>,
## # COUNTRY <chr>, NAICS_CODE <dbl>, NAICS_DESC <chr>, SOURCE <chr>,
## # SOURCEDATE <dttm>, VAL_METHOD <chr>, VAL_DATE <dttm>, WEBSITE <chr>,
## # STFIPS <chr>, COFIPS <chr>, SECTOR <dbl>, LEVEL_ <dbl>, HI_OFFER <dbl>,
## # DEG_GRANT <dbl>, LOCALE <dbl>, CLOSE_DATE <chr>, MERGE_ID <dbl>,
## # ALIAS <chr>, SIZE_SET <dbl>, INST_SIZE <dbl>, PT_ENROLL <dbl>, …
#make awesome icon list
iconSet <- awesomeIconList(
collegeIcon = makeAwesomeIcon(icon = 'university', markerColor = "green", library = 'fa', spin = FALSE),
juniorCollegeIcon = makeAwesomeIcon(icon = 'university', markerColor = "purple", library = 'fa', spin = FALSE)
)
leaflet(data = colleges) %>%
addProviderTiles(providers$CartoDB) %>%
addAwesomeMarkers(
icon = iconSet,
#hyperlink the website column
popup = ~paste0("<a href='", WEBSITE, "' target='_blank'>", NAME, "</a>"),
label = ~NAME,
clusterOptions = markerClusterOptions()
) %>%
#set legend based on names in NAICS_DESC column
addLegend(position = "bottomright",
labels = unique(colleges$NAICS_DESC),
title = "School Type",
colors = c("purple", "green"))
Fig 1. Leaflet map for Colleges and Junior Colleges by State.
#replace -999 value with NA
colleges$TOT_ENROLL <- ifelse(colleges$TOT_ENROLL == -999, NA, colleges$TOT_ENROLL)
#summarize enrollment by NAICS_DESC and STATE columns
enrollment_summary <- colleges %>%
group_by(NAICS_DESC, STATE) %>%
summarize(total_enrollment = sum(TOT_ENROLL, na.rm = TRUE)) %>%
arrange(desc(total_enrollment)) # Sort by highest enrollment
#display the first few rows of the summarized data
head(enrollment_summary)
## Simple feature collection with 6 features and 3 fields
## Geometry type: MULTIPOINT
## Dimension: XY
## Bounding box: xmin: -124.1951 ymin: 25.68367 xmax: -72.6991 ymax: 44.69309
## Geodetic CRS: WGS 84
## # A tibble: 6 × 4
## # Groups: NAICS_DESC [2]
## NAICS_DESC STATE total_enrollment geometry
## <chr> <chr> <dbl> <MULTIPOINT [°]>
## 1 JUNIOR COLLEGES CA 1510863 ((-122.3892 41.41385), (…
## 2 COLLEGES, UNIVERSITIES, AND … CA 1194859 ((-124.0773 40.87545), (…
## 3 COLLEGES, UNIVERSITIES, AND … NY 949994 ((-73.46666 44.69309), (…
## 4 COLLEGES, UNIVERSITIES, AND … TX 841596 ((-94.10375 33.49053), (…
## 5 JUNIOR COLLEGES TX 786137 ((-94.078 33.44187), (-9…
## 6 COLLEGES, UNIVERSITIES, AND … FL 611168 ((-87.19059 30.49622), (…