4/20/2021
remove(list = ls())
setwd("E:/R")
suppressWarnings(suppressMessages(library(leaflet)))
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(rgdal)))
suppressWarnings(suppressMessages(library(ggmap)))
suppressWarnings(suppressMessages(library(maps)))
suppressWarnings(suppressMessages(library(sf)))
suppressWarnings(suppressMessages(library(gapminder)))
suppressWarnings(suppressMessages(library(tidyverse)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(htmlwidgets)))
suppressWarnings(suppressMessages(library(htmltools)))
#Importing shapefile (currently unused)
ct_shapefile <- read_sf(dsn="ct_eco_13")
class(ct_shapefile)
## [1] "sf" "tbl_df" "tbl" "data.frame"
#Importing data
college <- read.csv("college.csv")
uscities <- read.csv("uscities.csv")
#Subsetting data:
ctcollege <- subset(college, state == 'CT')
ctcities <- subset(uscities, state_id == 'CT')
ctmajorcities <- subset(ctcities, population > 50000)
#Joining datasets for universities and cities:
AllCTdata <- left_join(ctcollege,ctcities,by="city")
#Creating df with only variables of interest:
df <- ctcollege[,c('lat','lng','name','undergrads', 'control', 'state', 'admission_rate', 'sat_avg')]
df <- df[complete.cases(df),]
#Creating palette for later use:
pal <- colorFactor(
palette = 'Dark2',
domain = df$control
)
#Creating population categories for legend (not optimal, will find another way):
df$popcategory <- df %>%
mutate(undergrads = case_when(undergrads < 5000 ~ "C",
undergrads <= 10000 ~ "B",
undergrads > 10000 ~ "A"))
#Creating custom legend function for the pop categories (not optimal):
addLegendCustom <- function(map, colors, title, labels, sizes, opacity = 0.5){
colorAdditions <- paste0(colors, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")
labelAdditions <- paste0("<div style='display: inline-block;height: ",
sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>",
labels, "</div>")
titleAdditions <- paste0(title = "Undergrad Population")
return(addLegend(map, colors = colorAdditions,
labels = labelAdditions, opacity = opacity))
}
#Creating custom tag for title with HTML:
tag.map.title <- tags$style(HTML("
.leaflet-control.map-title {
transform: translate(-50%,20%);
position: fixed !important;
left: 50%;
text-align: center;
padding-left: 10px;
padding-right: 10px;
background: rgba(255,255,255,0.75);
font-weight: bold;
font-size: 28px;
}
"))
title <- tags$div(
tag.map.title, HTML("Connecticut Colleges")
)
#Creating the map:
map = leaflet(df) %>% setView(lng = -72.8, lat = 41.55, zoom = 9) %>% addTiles() %>%
addCircleMarkers(radius = 0.005*(df$undergrads), stroke = FALSE, fillOpacity = 0.5, fillColor = ~pal(control), label = df$name) %>%
addLegend(data = df, title = "Institution Control", position = "bottomright", pal = pal, values = df$control) %>%
addLegendCustom(colors = c("black","black","black"), labels = c("<5,000 Undergrads", "5-10,000", ">10,000"),
sizes = c(15, 18, 22)) %>% addControl(title, position = "topleft", className="map-title")
## Assuming "lng" and "lat" are longitude and latitude, respectively
map