Load the libraries and set the working directory

The GeoLocation variable has (lat, long) format

Split GeoLocation (lat, long) into two columns: lat and long

latlong <- tidyr::extract(cities500, GeoLocation, c('lat', 'long'), 
               regex = ',?\\s*\\((\\d+\\.\\d+).*(-?\\d+\\.\\d+)\\)')
head(latlong)
## # A tibble: 6 × 25
##    Year StateA…¹ State…² CityN…³ Geogr…⁴ DataS…⁵ Categ…⁶ Uniqu…⁷ Measure Data_…⁸
##   <dbl> <chr>    <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
## 1  2017 CA       Califo… Hawtho… Census… BRFSS   Health… 063254… Arthri… %      
## 2  2017 CA       Califo… Hawtho… City    BRFSS   Unheal… 632548  Curren… %      
## 3  2017 CA       Califo… Hayward City    BRFSS   Health… 633000  Corona… %      
## 4  2017 CA       Califo… Hayward City    BRFSS   Unheal… 633000  Obesit… %      
## 5  2017 CA       Califo… Hemet   City    BRFSS   Preven… 633182  Choles… %      
## 6  2017 CA       Califo… Indio   Census… BRFSS   Health… 063644… Arthri… %      
## # … with 15 more variables: DataValueTypeID <chr>, Data_Value_Type <chr>,
## #   Data_Value <dbl>, Low_Confidence_Limit <dbl>, High_Confidence_Limit <dbl>,
## #   Data_Value_Footnote_Symbol <chr>, Data_Value_Footnote <chr>,
## #   PopulationCount <dbl>, lat <chr>, long <chr>, CategoryID <chr>,
## #   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>,
## #   Short_Question_Text <chr>, and abbreviated variable names ¹​StateAbbr,
## #   ²​StateDesc, ³​CityName, ⁴​GeographicLevel, ⁵​DataSource, ⁶​Category, …

Filter the dataset

Remove the StateDesc that includes the United Sates, select Prevention as the category (of interest), filter for only measuring crude prevalence and select only 2017.

latlong_clean <- latlong |>
  filter(StateDesc != "United States") |>
  filter(Category == "Prevention") |>
  filter(Data_Value_Type == "Crude prevalence") |>
  filter(Year == 2017)
head(latlong_clean)
## # A tibble: 6 × 25
##    Year StateA…¹ State…² CityN…³ Geogr…⁴ DataS…⁵ Categ…⁶ Uniqu…⁷ Measure Data_…⁸
##   <dbl> <chr>    <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>  
## 1  2017 AL       Alabama Montgo… City    BRFSS   Preven… 151000  Choles… %      
## 2  2017 CA       Califo… Concord City    BRFSS   Preven… 616000  Visits… %      
## 3  2017 CA       Califo… Concord City    BRFSS   Preven… 616000  Choles… %      
## 4  2017 CA       Califo… Fontana City    BRFSS   Preven… 624680  Visits… %      
## 5  2017 CA       Califo… Richmo… Census… BRFSS   Preven… 066062… Choles… %      
## 6  2017 FL       Florida Davie   Census… BRFSS   Preven… 121647… Choles… %      
## # … with 15 more variables: DataValueTypeID <chr>, Data_Value_Type <chr>,
## #   Data_Value <dbl>, Low_Confidence_Limit <dbl>, High_Confidence_Limit <dbl>,
## #   Data_Value_Footnote_Symbol <chr>, Data_Value_Footnote <chr>,
## #   PopulationCount <dbl>, lat <chr>, long <chr>, CategoryID <chr>,
## #   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>,
## #   Short_Question_Text <chr>, and abbreviated variable names ¹​StateAbbr,
## #   ²​StateDesc, ³​CityName, ⁴​GeographicLevel, ⁵​DataSource, ⁶​Category, …

What variables are included? (can any of them be removed?)

names(latlong_clean)
##  [1] "Year"                       "StateAbbr"                 
##  [3] "StateDesc"                  "CityName"                  
##  [5] "GeographicLevel"            "DataSource"                
##  [7] "Category"                   "UniqueID"                  
##  [9] "Measure"                    "Data_Value_Unit"           
## [11] "DataValueTypeID"            "Data_Value_Type"           
## [13] "Data_Value"                 "Low_Confidence_Limit"      
## [15] "High_Confidence_Limit"      "Data_Value_Footnote_Symbol"
## [17] "Data_Value_Footnote"        "PopulationCount"           
## [19] "lat"                        "long"                      
## [21] "CategoryID"                 "MeasureId"                 
## [23] "CityFIPS"                   "TractFIPS"                 
## [25] "Short_Question_Text"

Remove the variables that will not be used in the assignment

prevention <- latlong_clean |>
  select(-DataSource,-Data_Value_Unit, -DataValueTypeID, -Low_Confidence_Limit, -High_Confidence_Limit, -Data_Value_Footnote_Symbol, -Data_Value_Footnote)
head(prevention)
## # A tibble: 6 × 18
##    Year StateA…¹ State…² CityN…³ Geogr…⁴ Categ…⁵ Uniqu…⁶ Measure Data_…⁷ Data_…⁸
##   <dbl> <chr>    <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>     <dbl>
## 1  2017 AL       Alabama Montgo… City    Preven… 151000  Choles… Crude …    80.2
## 2  2017 CA       Califo… Concord City    Preven… 616000  Visits… Crude …    64.5
## 3  2017 CA       Califo… Concord City    Preven… 616000  Choles… Crude …    80.1
## 4  2017 CA       Califo… Fontana City    Preven… 624680  Visits… Crude …    66.1
## 5  2017 CA       Califo… Richmo… Census… Preven… 066062… Choles… Crude …    81.8
## 6  2017 FL       Florida Davie   Census… Preven… 121647… Choles… Crude …    84.5
## # … with 8 more variables: PopulationCount <dbl>, lat <chr>, long <chr>,
## #   CategoryID <chr>, MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>,
## #   Short_Question_Text <chr>, and abbreviated variable names ¹​StateAbbr,
## #   ²​StateDesc, ³​CityName, ⁴​GeographicLevel, ⁵​Category, ⁶​UniqueID,
## #   ⁷​Data_Value_Type, ⁸​Data_Value

The new dataset “Prevention” is a manageable dataset now.

For your assignment, work with the cleaned “Prevention” dataset

1. Once you run the above code, filter this dataset one more time for any particular subset.

Filtering for access to health insurance in Kansas

# Selecting prevention measure, ACCESS2 (the prevalence of current lack of health insurance among adults aged 18-64 years) for the state of Kansas at the census tract geographic level.

prevention_ks<- prevention|>
  filter(StateAbbr=="KS" & GeographicLevel== "Census Tract" & MeasureId == "ACCESS2"& !is.na(Data_Value))
head(prevention_ks)
## # A tibble: 6 × 18
##    Year StateA…¹ State…² CityN…³ Geogr…⁴ Categ…⁵ Uniqu…⁶ Measure Data_…⁷ Data_…⁸
##   <dbl> <chr>    <chr>   <chr>   <chr>   <chr>   <chr>   <chr>   <chr>     <dbl>
## 1  2017 KS       Kansas  Topeka  Census… Preven… 207100… "Curre… Crude …    24.3
## 2  2017 KS       Kansas  Wichita Census… Preven… 207900… "Curre… Crude …    13.5
## 3  2017 KS       Kansas  Wichita Census… Preven… 207900… "Curre… Crude …    17.3
## 4  2017 KS       Kansas  Topeka  Census… Preven… 207100… "Curre… Crude …    19.5
## 5  2017 KS       Kansas  Lawren… Census… Preven… 203890… "Curre… Crude …    15.7
## 6  2017 KS       Kansas  Olathe  Census… Preven… 205257… "Curre… Crude …    12.3
## # … with 8 more variables: PopulationCount <dbl>, lat <chr>, long <chr>,
## #   CategoryID <chr>, MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>,
## #   Short_Question_Text <chr>, and abbreviated variable names ¹​StateAbbr,
## #   ²​StateDesc, ³​CityName, ⁴​GeographicLevel, ⁵​Category, ⁶​UniqueID,
## #   ⁷​Data_Value_Type, ⁸​Data_Value

2. Based on the GIS tutorial (Japan earthquakes), create one plot about something in your subsetted dataset.

ggplot(prevention_ks, aes(Data_Value, PopulationCount, color = CityName)) +
  geom_point()+
  labs(title = "Percent of Adults Aged 18-64 Years Without Access to Health Insurance in Kansas, 2017",
       x = "Prevalence Rate (%)",
       y = "Population",
       color = "Cites") +
  theme_minimal()+
  scale_color_brewer(palette = "Accent")

The plot above shows that Kansas City, Topeka and Wichita have some of the highest prevalence of adults aged 18-64 years without health insurance. The prevalence of adults without health insurance is generally lowest in Overland Park.

3. Now create a map of your subsetted dataset.

#load library
library(leaflet)
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(leaflegend)

#viewing structure of Kansas subset
str(prevention_ks)
## tibble [298 × 18] (S3: tbl_df/tbl/data.frame)
##  $ Year               : num [1:298] 2017 2017 2017 2017 2017 ...
##  $ StateAbbr          : chr [1:298] "KS" "KS" "KS" "KS" ...
##  $ StateDesc          : chr [1:298] "Kansas" "Kansas" "Kansas" "Kansas" ...
##  $ CityName           : chr [1:298] "Topeka" "Wichita" "Wichita" "Topeka" ...
##  $ GeographicLevel    : chr [1:298] "Census Tract" "Census Tract" "Census Tract" "Census Tract" ...
##  $ Category           : chr [1:298] "Prevention" "Prevention" "Prevention" "Prevention" ...
##  $ UniqueID           : chr [1:298] "2071000-20177004000" "2079000-20173009605" "2079000-20173002800" "2071000-20177000700" ...
##  $ Measure            : chr [1:298] "Current lack of health insurance among adults aged 18\x9664 Years" "Current lack of health insurance among adults aged 18\x9664 Years" "Current lack of health insurance among adults aged 18\x9664 Years" "Current lack of health insurance among adults aged 18\x9664 Years" ...
##  $ Data_Value_Type    : chr [1:298] "Crude prevalence" "Crude prevalence" "Crude prevalence" "Crude prevalence" ...
##  $ Data_Value         : num [1:298] 24.3 13.5 17.3 19.5 15.7 12.3 11.3 12.9 28.6 13 ...
##  $ PopulationCount    : num [1:298] 3066 2405 3095 3131 2476 ...
##  $ lat                : chr [1:298] "39.0483238175" "37.6354637105" "37.680761443" "39.0829919471" ...
##  $ long               : chr [1:298] "5.6722356718" "7.4578052468" "7.3636650345" "5.6956598691" ...
##  $ CategoryID         : chr [1:298] "PREVENT" "PREVENT" "PREVENT" "PREVENT" ...
##  $ MeasureId          : chr [1:298] "ACCESS2" "ACCESS2" "ACCESS2" "ACCESS2" ...
##  $ CityFIPS           : num [1:298] 2071000 2079000 2079000 2071000 2038900 ...
##  $ TractFIPS          : num [1:298] 2.02e+10 2.02e+10 2.02e+10 2.02e+10 2.00e+10 ...
##  $ Short_Question_Text: chr [1:298] "Health Insurance" "Health Insurance" "Health Insurance" "Health Insurance" ...
#converting lat and long from numeric to string.
prevention_ks$lat <- as.numeric(prevention_ks$lat)
prevention_ks$long <- as.numeric(prevention_ks$long)

#correcting the longitude coordinate values
prevention_ks$long <- -(prevention_ks$long+90)


#Setting the latitude and longitude values for Kansas
ks_lat<- 39.0119
ks_lon<- -98.4842

#reading in the shapefile with 2017 population data for Kansas
data_map <- read_sf("cb_2017_20_tract_500k.shp")
data_map <- st_transform(data_map, crs = '+proj=longlat +datum=WGS84')
data_map$GEOID <- as.numeric(data_map$GEOID)

#Joining the shapefile for Kansas with the prevention data
join_data<- left_join(prevention_ks, data_map, by = c('TractFIPS' = 'GEOID'))

## creating a color palette for the map
pal <- colorNumeric(
  palette = "YlOrRd",
  domain = join_data$Data_Value
)

#Creating the map
leaflet() |>
  setView(lng = ks_lon, lat = ks_lat, zoom = 6) |>
  addProviderTiles(provider = "Stadia.AlidadeSmooth")|>
  addPolygons(data=join_data$geometry, stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, fillColor = pal(join_data$Data_Value))|>
 addLegend(data = join_data$geometry,
            position = "bottomright",
           pal = pal, 
            values = join_data$Data_Value,
            title = "Percent Adults with No Health Insurance",
           labFormat = labelFormat(suffix= "%"),
            opacity = 1)

4. Refine your map to include a mouse over tooltip

#creating the popup
popupks <- paste0("<b>City Name: </b>", join_data$CityName, "<br>", "<b>Population Count: </b>",  join_data$PopulationCount, "<br>", "<b>No Insurance (%): </b>", join_data$Data_Value, "<br>")


#Adding popup to the map
leaflet() |>
  setView(lng = ks_lon, lat = ks_lat, zoom = 6) |>
  addProviderTiles(provider = "Stadia.AlidadeSmooth")|>
  addPolygons(data=join_data$geometry, stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, fillColor = pal(join_data$Data_Value), popup = popupks)|>
 addLegend(data = join_data$geometry,
            position = "bottomright",
           pal = pal, 
            values = join_data$Data_Value,
            title = "Percent Adults with No Health Insurance",
           labFormat = labelFormat(suffix= "%"),
            opacity = 1)

5. Write a paragraph

The map shows the prevalence rate of adults aged 18-64 years that lack health insurance in Kansas. Some of the highest prevalence of uninsured adults can be found in Kansas City, Topeka and Wichita. While there are areas where more than 10% of the adult population are without health insurance in Lawrence, Olathe, and Overland Park, in general, these cities appear to have the lowest rates of adults without health insurance. It should be noted Olathe and Overland Park are in the wealthiest county in Kansas, Johnson County, which may explain why there are lower rates of uninsured adults.