Purpose of the Project

The purpose of this project is to try and uncover exactly what the correlation is between radio stations in the mid-state area along with the population density, income, education, age, etc. As someone who works in the radio industry in the mid-state of Tennessee, I want to see why some areas seem to be overflowing with radio coverage while others do not get nearly the attention that they perhaps could. The idea is to establish the contours that these radio stations fall under from an area perspective and uncover how some areas compare to others.

Data Source

The Data Source that I used for this particular project was that of https://radio-locator.com/ which serves as a way to find any radio station across the country. It was the perfect source for this project as it gave me the exact information needed to figure out where radio stations and are and begin to establish what exact area they cover in the mid-state of Tennessee. What it does is allow you to put in basic information on radio stations and vague terms, and it will find that station for you. This made the research process much simpler and expedited the process of finding the necessary data. Of course, that was not the only important component, as my Census API key allowed to me to uncover the specific population details for the contours as well and then begin to establish the relationship between these different components that I mentioned earlier.

Output

The output that I got for this project was not easy to get to say the least. There were many different problems that I had trying to translate code from Microsoft Copilot into RStudio, but eventually through a lot of troubleshooting, I was able to get the data that I needed.

From the script that I have in place, it produces the one large map that takes into account all of the previous factors that I mentioned which needed to be included on this script. It was key to make sure that the map that was produced was done by plotly however, as it had to be interactive as well as detailed. The original output I had actually produced several different maps that each covered the individual components of income, population, etc. However, I felt as though this was unnecessary if all of it could be featured in one map, and this worked to phase out some of the excessive reading and analyzing that would have needed to be done otherwise.

The map itself is shaded blue but features the borders of every county in the mid-state along with the striking orange circles. These circles are the range of the different radio stations in the area and just how far they extend out. Some of them extend out not just through most of the mid-state but also all the way into northern Kentucky, which why some of the circles go off the map. There is also a bar on the side that demonstrates the population density of a given area through a color code from dark blue to yellow. It extends out to over 15,000 while it starts all the way down at 0. When all is put together, it paints a great picture of just what the radio station picture looks like the in the mid-state.

My Role in Developing the Script

While Microsoft Copilot developed the script, I had a heavy role in getting it organized and troubleshooting the script until it worked in both the main R Script as well as R Markdown.

Things I Learned While Putting the Project Together

There were many things that I took away while putting it together, and it starts with making sure to be specific with your issues on Copilot. While it is a great tool, it is not all-knowing and is going to miss details if you do not specify exactly what kind of troubles that you are coming away with. You have to be ready and willing to share what kind of problems you are having and do so in a clear manner.

I learned a lot more about how to put more details into map and just what goes into a plotly setup with many different kinds of ACS variables to consider. In the process, I also learned what it takes to put all of those concepts together, and create something that merges ACS variables and radio station data into one to create a map.

It also helped to learn more about map customization and how to add and take away specific colors and add specifications such as the map bar on the side to add context. I also had to use this knowledge to find out how to apply tracts and then counties to R data which I was able to do thanks to Copilot’s help. Finally, I was able to see just how important it is at times to use tracts instead of county’s when putting together a geographic map and that shown through well through my final data. It helped tremendously to have the tracts and better reduce area for better and more clear results on where radio stations are prevalent and where they are not.

How the Script Works

This description below is created by Copilot but edited by me and demonstrates exactly how this script operates.

The Complete Script

Below is the complete and final script showing how all of this comes together and what it takes to get the final result of the map with the data on the reality of radio stations in the mid-state of Tennessee. One will need to have a Census API key to access this data if they want to make a script of their own like this one.

#################################################
# LIBRARIES
#################################################

library(tidyverse)
library(sf)
library(tigris)
library(jsonlite)
library(tidycensus)
library(plotly)
library(scales)
library(viridis)

#################################################
# CENSUS API KEY
#################################################

census_api_key(
  "33060ddff9e66f647b10ef7b540ab19f3dd5e9c4",
  install = FALSE
)

#################################################
# MIDDLE TENNESSEE COUNTIES
#################################################

middle_tn <- c(
  "Bedford","Cannon","Cheatham","Clay","Coffee",
  "Davidson","DeKalb","Dickson","Fentress","Franklin",
  "Giles","Grundy","Hickman","Houston","Humphreys",
  "Jackson","Lawrence","Lewis","Lincoln","Macon",
  "Marshall","Maury","Montgomery","Moore","Overton",
  "Perry","Pickett","Putnam","Robertson","Rutherford",
  "Smith","Stewart","Sumner","Trousdale","Van Buren",
  "Warren","Wayne","White","Williamson","Wilson"
)

#################################################
# DOWNLOAD COUNTY BOUNDARIES
#################################################

tn_counties <- counties(
  state = "TN",
  cb = TRUE,
  year = 2024
) |>
  st_transform(4326)

middle_tn_counties <- tn_counties |>
  filter(NAME %in% middle_tn)

#################################################
# DOWNLOAD STATION LIST
#################################################

url <- "https://raw.githubusercontent.com/dks4a/Middle-Tennessee-Radio-Station-Data/refs/heads/main/middle_tennessee_radio_stations.csv"

station_data <- read_csv(url)

working_calls <- unique(
  station_data$CallSign
)

#################################################
# FCC CONTOUR DOWNLOAD FUNCTION
#################################################

get_station_sf <- function(callsign){
  
  attempts <- expand.grid(
    service = c("fm","am"),
    call = c(
      callsign,
      paste0(callsign,"-FM")
    ),
    stringsAsFactors = FALSE
  )
  
  for(i in seq_len(nrow(attempts))){
    
    service <- attempts$service[i]
    cs <- attempts$call[i]
    
    url <- paste0(
      "https://geo.fcc.gov/api/contours/entity.json?",
      "serviceType=",service,
      "&callsign=",cs,
      "&nradial=360",
      "&field=60",
      "&curve=0",
      "&pop=false",
      "&area=false"
    )
    
    result <- suppressWarnings(
      try(
        fromJSON(url),
        silent = TRUE
      )
    )
    
    if(inherits(result,"try-error")) next
    
    if(
      is.null(result$features) ||
      length(result$features)==0
    ) next
    
    coords <- try(
      result$features$geometry$coordinates[[1]][1,1,,],
      silent = TRUE
    )
    
    if(inherits(coords,"try-error")) next
    
    return(
      st_sf(
        CallSign = callsign,
        geometry = st_sfc(
          st_polygon(list(coords)),
          crs = 4326
        )
      )
    )
  }
  
  return(NULL)
}

#################################################
# DOWNLOAD FCC CONTOURS
#################################################

contour_list <- lapply(
  working_calls,
  get_station_sf
)

contour_list <- contour_list[
  !sapply(
    contour_list,
    is.null
  )
]

fm_contours <- do.call(
  rbind,
  contour_list
)

#################################################
# VERIFY CONTOURS
#################################################

print(class(fm_contours))
print(nrow(fm_contours))

#################################################
# COUNTY POPULATION
#################################################

county_pop <- get_acs(
  geography = "county",
  state = "TN",
  variables = "B01003_001",
  year = 2023,
  geometry = TRUE
) |>
  st_transform(4326)

county_pop_mt <- county_pop |>
  filter(
    sub(
      " County, Tennessee",
      "",
      NAME
    ) %in% middle_tn
  )

#################################################
# COUNTY LABELS
#################################################

county_labels <- st_point_on_surface(
  county_pop_mt
)

county_coords <- cbind(
  county_labels,
  st_coordinates(
    county_labels
  )
)

#################################################
# COUNTY MAP
#################################################

ggplot() +
  
  geom_sf(
    data = county_pop_mt,
    aes(fill = estimate),
    color = "white"
  ) +
  
  geom_sf(
    data = fm_contours,
    fill = NA,
    color = "orange",
    linewidth = .6
  ) +
  
  geom_text(
    data = county_coords,
    aes(
      X,
      Y,
      label = sub(
        " County, Tennessee",
        "",
        NAME
      )
    ),
    size = 2.5
  ) +
  
  scale_fill_viridis_c(
    labels = comma
  ) +
  
  labs(
    title = "Middle Tennessee Population and Radio Coverage"
  ) +
  
  theme_minimal()

#################################################
# ACS TRACT VARIABLES
#################################################

acs_vars <- get_acs(
  geography = "tract",
  state = "TN",
  variables = c(
    Population = "B01003_001",
    Income = "B19013_001",
    Age = "B01002_001",
    Education = "B15003_022"
  ),
  year = 2023,
  geometry = TRUE
)

#################################################
# WIDE FORMAT
#################################################

tract_demo <- acs_vars |>
  
  st_transform(4326) |>
  
  select(
    GEOID,
    variable,
    estimate,
    geometry
  ) |>
  
  pivot_wider(
    names_from = variable,
    values_from = estimate
  )

#################################################
# KEEP MIDDLE TN TRACTS
#################################################

tract_demo <- tract_demo |>
  
  mutate(
    COUNTYFP = substr(
      GEOID,
      3,
      5
    )
  ) |>
  
  filter(
    COUNTYFP %in%
      middle_tn_counties$COUNTYFP
  )

#################################################
# POPULATION DENSITY
#################################################

tract_demo_proj <- st_transform(
  tract_demo,
  5070
)

tract_demo$area_sq_miles <-
  as.numeric(
    st_area(
      tract_demo_proj
    )
  ) / 2589988.11

tract_demo$pop_density <-
  tract_demo$Population /
  tract_demo$area_sq_miles

#################################################
# STATION COVERAGE COUNTS
#################################################

tract_points <- st_point_on_surface(
  tract_demo
)

tract_points$station_count <- lengths(
  st_intersects(
    tract_points,
    fm_contours
  )
)

tract_demo <- tract_demo |>
  
  left_join(
    st_drop_geometry(
      tract_points |>
        select(
          GEOID,
          station_count
        )
    ),
    by = "GEOID"
  )

#################################################
# HOVER LABELS
#################################################

tract_demo <- tract_demo |>
  
  mutate(
    hover_text = paste0(
      "<b>Tract:</b> ", GEOID,
      "<br><b>Population:</b> ",
      comma(Population),
      "<br><b>Density:</b> ",
      round(pop_density,1),
      " persons/sq mi",
      "<br><b>Income:</b> $",
      comma(Income),
      "<br><b>Age:</b> ",
      round(Age,1),
      "<br><b>Education:</b> ",
      comma(Education),
      "<br><b>Stations:</b> ",
      station_count
    )
  )

#################################################
# INTERACTIVE DEMOGRAPHIC MAP
#################################################

p <- ggplot() +
  
  geom_sf(
    data = tract_demo,
    aes(
      fill = pop_density,
      text = hover_text
    ),
    color = NA
  ) +
  
  geom_sf(
    data = fm_contours,
    fill = NA,
    color = "orange",
    linewidth = .4
  ) +
  
  scale_fill_viridis_c(
    option = "C",
    labels = comma,
    name = "Population Density"
  ) +
  
  labs(
    title = "Middle Tennessee Census Tracts",
    subtitle = "Population Density, Income, Age, Education and Radio Coverage"
  ) +
  
  theme_minimal()

ggplotly(
  p,
  tooltip = "text"
)

#################################################
# STATION COVERAGE MAP
#################################################

ggplot() +
  
  geom_sf(
    data = tract_demo,
    aes(fill = station_count),
    color = NA
  ) +
  
  geom_sf(
    data = fm_contours,
    fill = NA,
    color = "orange"
  ) +
  
  scale_fill_viridis_c() +
  
  labs(
    title = "Radio Station Coverage by Census Tract"
  ) +
  
  theme_minimal()