#Install and load the necessary libraries library(tidycensus) #For accessing U.S. Census data library(sf) #For working with spatial data library(tmap) #For creating thematic maps

#Set Census API key to access ACS data census_api_key(“b0fa245cb3bd0841e67029c155067c6c2f34de0a”, overwrite = TRUE) #Replace “your_census_api_key_here” with your valid Census API key

#Fetch Gini Index data for all U.S. counties income_inequality_data <- get_acs( geography = “county”, # Specify geography level: County-level data variables = “B19083_001”, # Gini Index variable from ACS year = 2020, # Specify the year of the data (2020 ACS) geometry = TRUE # Include spatial geometries for mapping )

#Inspect the structure of the retrieved data head(income_inequality_data) #Check the first few rows to ensure data accuracy

#Set tmap mode to static plotting tmap_mode(“plot”) #Use “plot” mode for static, publication-ready maps

#Create the choropleth map tm_shape(income_inequality_data) + # Set the data layer to the Gini Index dataset tm_polygons( col = “estimate”, #Use the Gini Index values as the color variable palette = “Reds”, #Apply a red color gradient (light to dark red) style = “quantile”, #Classify data into quantiles for better distribution title = “Income Inequality (Gini Index)” #Add a descriptive legend title ) + tm_layout( title = “Income Inequality Across U.S. Counties (2020)”, #Add a map title frame = FALSE #Remove the map frame for a clean look ) + tm_borders(lwd = 0.1, col = “white”) #Add subtle white borders to separate counties

#The final map will display income inequality (measured by the Gini Index) #across U.S. counties using a quantile classification and a red gradient palette.