library(tidyverse)
library(rvest)
url <- "https://www.florida-demographics.com/counties_by_population"
h <- read_html(url)
tab <- h %>% html_nodes("table")
tab <- tab %>% html_table
FloridaDemographics <- as.data.frame(tab)
FloridaDemographics$Rank <- as.numeric(FloridaDemographics$Rank)
## Warning: NAs introduced by coercion
FloridaDemographics$Population <- parse_number(FloridaDemographics$Population)
## Warning: 1 parsing failure.
## row col expected actual
## 68 -- a number .
FloridaDemographics <- FloridaDemographics[-c(68),]
library(maps)
library(ggmap)
library(mapdata)
states <- map_data("state")
FL_df <- subset(states, region == "florida")
counties <- map_data("county")
FL_counties <- subset(counties, region == "florida")
FL_counties$county <- FL_counties$subregion
FL_counties$county <- paste(FL_counties$county, "County" )
simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep="", collapse=" ")
}
FL_counties$county <- sapply(FL_counties$county, simpleCap)
colnames(FL_counties)[7] <- "County"
Florida_merged <- inner_join(FloridaDemographics, FL_counties, by = "County")
unique(Florida_merged$Rank)
## [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 25 26 27 28
## [26] 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 49 50 51 52 53 54
## [51] 55 56 57 58 59 60 61 62 63 64 65 66 67
FloridaDemographics
## Rank County Population
## 1 1 Miami-Dade County 2715516
## 2 2 Broward County 1909151
## 3 3 Palm Beach County 1446277
## 4 4 Hillsborough County 1378883
## 5 5 Orange County 1321194
## 6 6 Pinellas County 957875
## 7 7 Duval County 924229
## 8 8 Lee County 718679
## 9 9 Polk County 668671
## 10 10 Brevard County 576808
## 11 11 Volusia County 527634
## 12 12 Pasco County 510593
## 13 13 Seminole County 455086
## 14 14 Sarasota County 412144
## 15 15 Manatee County 373853
## 16 16 Collier County 363922
## 17 17 Marion County 348371
## 18 18 Osceola County 338619
## 19 19 Lake County 335362
## 20 20 Escambia County 311522
## 21 21 St. Lucie County 305591
## 22 22 Leon County 288102
## 23 23 Alachua County 263148
## 24 24 St. Johns County 235503
## 25 25 Clay County 207291
## 26 26 Okaloosa County 200737
## 27 27 Hernando County 182696
## 28 28 Bay County 182482
## 29 29 Charlotte County 176954
## 30 30 Santa Rosa County 170442
## 31 31 Martin County 157581
## 32 32 Indian River County 150984
## 33 33 Citrus County 143087
## 34 34 Sumter County 120999
## 35 35 Flagler County 107139
## 36 36 Highlands County 102101
## 37 37 Nassau County 80578
## 38 38 Monroe County 76325
## 39 39 Putnam County 72766
## 40 40 Columbia County 69105
## 41 41 Walton County 65858
## 42 42 Jackson County 48472
## 43 43 Gadsden County 46017
## 44 44 Suwannee County 43924
## 45 45 Okeechobee County 40572
## 46 46 Hendry County 40127
## 47 47 Levy County 39961
## 48 48 DeSoto County 36399
## 49 49 Wakulla County 31877
## 50 50 Baker County 27785
## 51 51 Hardee County 27228
## 52 52 Bradford County 26979
## 53 53 Washington County 24566
## 54 54 Taylor County 22098
## 55 55 Holmes County 19430
## 56 56 Madison County 18474
## 57 57 Gilchrist County 17615
## 58 58 Dixie County 16437
## 59 59 Gulf County 16055
## 60 60 Union County 15239
## 61 61 Calhoun County 14444
## 62 62 Hamilton County 14269
## 63 63 Jefferson County 14105
## 64 64 Glades County 13363
## 65 65 Franklin County 11736
## 66 66 Lafayette County 8744
## 67 67 Liberty County 8365
FL_counties$County[FL_counties$County == "Miami-dade County"] <- "Miami-Dade County"
FL_counties$County[FL_counties$County == "St Lucie County"] <- "St. Lucie County"
FL_counties$County[FL_counties$County == "St Johns County"] <- "St. Johns County"
FL_counties$County[FL_counties$County == "De Soto County"] <- "DeSoto County"
Florida_merged <- inner_join(FloridaDemographics, FL_counties, by = "County")
length(unique(Florida_merged$Rank))
## [1] 67
Fl_base <- ggplot(data = Florida_merged, mapping = aes(x = long, y = lat, group = group))+
coord_fixed(1.3)+
geom_polygon(color = "black", fill = "gray")
Fl_base +
geom_polygon(aes(fill = Population), color = "white")
library(scales)
Fl_base +
geom_polygon(aes(fill = Population), color = "white")+
scale_fill_gradient(labels = comma, breaks = c(0, 500000, 1000000, 1500000, 2000000, 2500000, 3000000))