Load the libraries and set the working directory

library(tidyverse)
library(tidyr)
library(maps)
library(mapdata)
library(leaflet)
setwd("/Users/andreassanches/Documents")
cities500 <- read_csv("500CitiesLocalHealthIndicators.cdc.csv")

The GeoLocation variable has (lat, long) format

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

latlong2 <- cities500|>
  mutate(GeoLocation = str_replace_all(GeoLocation, "[()]", ""))|>
  separate(GeoLocation, into = c("lat", "long"), sep = ",", convert = TRUE)
head(latlong2)
## # A tibble: 6 × 25
##    Year StateAbbr StateDesc  CityName  GeographicLevel DataSource Category      
##   <dbl> <chr>     <chr>      <chr>     <chr>           <chr>      <chr>         
## 1  2017 CA        California Hawthorne Census Tract    BRFSS      Health Outcom…
## 2  2017 CA        California Hawthorne City            BRFSS      Unhealthy Beh…
## 3  2017 CA        California Hayward   City            BRFSS      Health Outcom…
## 4  2017 CA        California Hayward   City            BRFSS      Unhealthy Beh…
## 5  2017 CA        California Hemet     City            BRFSS      Prevention    
## 6  2017 CA        California Indio     Census Tract    BRFSS      Health Outcom…
## # ℹ 18 more variables: UniqueID <chr>, Measure <chr>, Data_Value_Unit <chr>,
## #   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 <dbl>, long <dbl>, CategoryID <chr>,
## #   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

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 <- latlong2 |>
  filter(StateDesc != "United States") |>
  filter(Category == "Prevention") |>
  filter(Data_Value_Type == "Crude prevalence") |>
  filter(Year == 2017)
head(latlong_clean)
## # A tibble: 6 × 25
##    Year StateAbbr StateDesc  CityName   GeographicLevel DataSource Category  
##   <dbl> <chr>     <chr>      <chr>      <chr>           <chr>      <chr>     
## 1  2017 AL        Alabama    Montgomery City            BRFSS      Prevention
## 2  2017 CA        California Concord    City            BRFSS      Prevention
## 3  2017 CA        California Concord    City            BRFSS      Prevention
## 4  2017 CA        California Fontana    City            BRFSS      Prevention
## 5  2017 CA        California Richmond   Census Tract    BRFSS      Prevention
## 6  2017 FL        Florida    Davie      Census Tract    BRFSS      Prevention
## # ℹ 18 more variables: UniqueID <chr>, Measure <chr>, Data_Value_Unit <chr>,
## #   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 <dbl>, long <dbl>, CategoryID <chr>,
## #   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

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 StateAbbr StateDesc  CityName  GeographicLevel Category UniqueID Measure
##   <dbl> <chr>     <chr>      <chr>     <chr>           <chr>    <chr>    <chr>  
## 1  2017 AL        Alabama    Montgome… City            Prevent… 151000   Choles…
## 2  2017 CA        California Concord   City            Prevent… 616000   Visits…
## 3  2017 CA        California Concord   City            Prevent… 616000   Choles…
## 4  2017 CA        California Fontana   City            Prevent… 624680   Visits…
## 5  2017 CA        California Richmond  Census Tract    Prevent… 0660620… Choles…
## 6  2017 FL        Florida    Davie     Census Tract    Prevent… 1216475… Choles…
## # ℹ 10 more variables: Data_Value_Type <chr>, Data_Value <dbl>,
## #   PopulationCount <dbl>, lat <dbl>, long <dbl>, CategoryID <chr>,
## #   MeasureId <chr>, CityFIPS <dbl>, TractFIPS <dbl>, Short_Question_Text <chr>

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.

I’m interested in looking for adults who took medicine for high blood pressure in Florida. Then, I selected the top 7 cities more populated in Florida.

florida_prevent <- prevention |> filter(StateDesc == "Florida" & Measure == "Taking medicine for high blood pressure control among adults aged >=18 Years with high blood pressure") |> arrange(desc(PopulationCount)) |> slice(1:7)

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

Now, I created my first visualization. I’m showing the percentage of adults with high blood pressure taking medicine to treat this health problem in Florida’s 7 largest cities. I labeled both axes and changed the default theme.

ggplot(florida_prevent, aes(x = Data_Value, y = PopulationCount, color = CityName)) + geom_point(size = 3, alpha = 0.8) +
         labs (title = "High Blood Pressure Medication Use in Adults \nWith High Blood Pressure in Florida's 7 Largest Cities", x = "Percentage Rate", y = "Population", color = "City Name") +
  scale_y_continuous(labels = scales::comma) +
  scale_color_brewer(palette = "Set1") +
  theme_gray()

3. Now create a map of your subsetted dataset.

Here I created my map. I used the “leaflet” library that allows me to create interactive maps. Then, I specified that the lat argument should be populated with values from the “lat” column, as I did with the “long” column. Finally, I specified the initial center of my map by using “lng = -81.5158 and lat = 27.6648”.

map_plot <- leaflet(florida_prevent) |>
  addTiles()  

map_plot <- map_plot |>
  addMarkers(data = florida_prevent, lat = ~lat, lng = ~long)


map_plot <- map_plot |>
  setView(lng = -81.5158, lat = 27.6648, zoom = 7)


map_plot

4. Refine your map to include a mousover tooltip

Here, I only added some tooltips to my graph, specifically the city name, population, latitude, longitude and the data value. Therefore, when you move the cursor over any of the 7 cities, this information will be displayed.

map_plot <- leaflet(florida_prevent) |>
  addTiles() 

map_plot <- map_plot |>
  addMarkers(data = florida_prevent, lat = ~lat, lng = ~long, 
             popup = ~paste("City: ", CityName, "<br>",
                             "Population: ", PopulationCount, "<br>",
                             "Latitude: ", lat, "<br>",
                             "Longitude: ", long, "<br>",
                             "Data Value: ", Data_Value))

map_plot <- map_plot |>
  setView(lng = -81.5158, lat = 27.6648, zoom = 5)

map_plot

5. Write a paragraph

In a paragraph, describe the plots you created and what they show.

The first plot, which is a scatterplot, shows the percentage of adults (>= 18 years) with high blood pressure that take medicine to treat this condition in the 7 largest Florida cities. Then, I created my first map, which basically highlights the location of these cities in Florida state and shows these cities from a global perspective. Finally, I added some tooltips to my first graph, which added some important information that made the visualization look better and easier to understand what is going on, such as the population of each city and the data (percentage) of adults taking medicine to treat their condition.