Creating Maps to display Universities in Florida

By: Amanda Sanfilippo

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.4
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.0.3
library(maps)
## Warning: package 'maps' was built under R version 4.0.4
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.0.5
college <- read.csv("college.csv")
uscities <- read.csv("uscities.csv")
FL <- filter(college, state=="FL")

FL2 <- filter(uscities, state_id=="FL")
collegecities <- left_join(FL, FL2, by= "city")
cities_pop <- arrange(collegecities, desc(population))

Using Leaflet

control_palette<-colorFactor("Dark2", unique(collegecities$control))

collegecities%>%
  leaflet()%>%
  addProviderTiles("Stamen.TonerHybrid")%>%
  addCircleMarkers(weight=5, label=~name,color=~control_palette(control), lng=~lng.x, lat=~lat.x, radius=~undergrads/1000,
                   popup=~paste("City:", city, "<br/>", "Undergrad pop:", undergrads))%>%
  addLegend("topright", pal = control_palette, values = ~control, title = "Control", opacity = 1)

With filter

cities_pop <- filter(cities_pop, population > 382720)

control_palette2<-colorFactor("Dark2", unique(cities_pop$control))

cities_pop%>%
  leaflet()%>%
  addProviderTiles("Stamen.TonerHybrid")%>%
  addCircleMarkers(weight=10, label=~name,color=~control_palette2(control),lng=~lng.x, lat=~lat.x, radius=~undergrads/1000,
                   popup=~paste("City:", city, "<br/>", "School:", name, "<br/>", 
                                "Undergrad pop:", undergrads))%>%
  addLegend("topright", pal = control_palette2, values = ~control, title = "Control", opacity = 1)

Using ggplot

my_state_map <- map_data("state")

FL3 <- filter(my_state_map, region=="florida")
ggplot(data=FL3, mapping=aes(x=long, y=lat, group=NULL))+
  geom_polygon(fill="white", color="black", alpha=0.3)+
  geom_point(data=cities_pop, aes(x=lng.x, y=lat.x, group=NULL, size=undergrads, color=control))+
  geom_text_repel( data=FL2 %>% arrange(population) %>% tail(5), aes(x=lng, y=lat, label=city), size=5)+
  ggtitle("Universities located in Florida's highest populated cities")

ggplot(data=FL3, mapping=aes(x=long, y=lat, group=NULL))+
  geom_polygon(fill="white", color="black")+
  geom_point(data=collegecities, aes(x=lng.x, y=lat.x, group=NULL, size=undergrads, color=control, alpha=0.3))+
  geom_text_repel( data=FL2 %>% arrange(population) %>% tail(5), aes(x=lng, y=lat, label=city), size=5)+
  ggtitle("Universities located in Florida", subtitle = "Source: US Department of Education")

ggplot(data=FL3, mapping=aes(x=long, y=lat, group=NULL))+
  geom_polygon(fill="white", color="black")+
  geom_point(data=collegecities, aes(x=lng.x, y=lat.x, group=NULL, size=undergrads, color=control, alpha=0.3))+
  geom_text_repel( data=FL2 %>% arrange(population) %>% tail(5), aes(x=lng, y=lat, label=city), size=5)+
  ggtitle("Universities located in Florida", subtitle = "Source: US Department of Education")+
  theme_map()

Acknowledgement

I would like to thank my Data Visualization & Communication professor, Gazi M. Duman, Ph.D., for teaching me how to create maps using Leaflet and ggplot.

About Me

Hi, I’m Amanda Sanfilippo. I am a senior at the University of New Haven majoring in Business Analytics.