# LOAD PACKAGES
library(pander)
library(kableExtra)
library(ggplot2)
library(scales)


Introduction

This code-through explores how to use the ggplot2 package to create a simple bar graph in R.

Learning Objectives

This code-through shows how to use ggplot2 to turn a small dataset into a simple bar graph. It focuses on the basic steps: importing data, choosing variables, creating bars, and labeling the graph so it is easier to understand.

Importing Data into RStudio

Homelessness data in the United States is collected and reported by the U.S. Department of Housing and Urban Development, also known as HUD. For this code-through, we will use HUD’s Point-in-Time Count data by state.

Finding Data: The data used in this example comes from HUD’s 2024 AHAR Part 1 page. On this page, HUD provides the 2007–2024 Point-in-Time Estimates by State file, which includes homelessness counts by state and year.

The original HUD PIT Count file includes multiple years of data in an Excel format. For this code-through, I saved the 2024 data as a CSV file and used only that year. This keeps the example simple and lets us focus on how to import data and make a graph with ggplot2.

After downloading and saving the PIT Count data as a CSV file, we can import it into RStudio. The head() function lets us check the first few rows of the dataset.

pit_data <- read.csv("pit_2024.csv")

# Clean the column used for the graph
pit_data$Overall.Homeless <- as.numeric(gsub(",", "", pit_data$Overall.Homeless))
pit_data <- pit_data[
  pit_data$State != "Total" & !is.na(pit_data$Overall.Homeless),
]
head(pit_data)

Creating the Bar Graph

This graph shows the 10 states with the highest homelessness counts in 2024.

top_states <- pit_data[order(-pit_data$Overall.Homeless), ][1:10, ]

ggplot(top_states, aes(x = reorder(State, -Overall.Homeless), y = Overall.Homeless)) +
  geom_col() +
  scale_y_continuous(labels = scales::comma) +
  labs(
    title = "Top 10 States by Homelessness Count in 2024",
    x = "State",
    y = "Number of People Experiencing Homelessness"
  ) +
  theme_minimal()

Results

The final graph shows the top 10 states with the highest homelessness counts in 2024. This makes it easier to compare homelessness across states in a simple visual format. While this code-through uses a simple example, the same approach could be extended to look at how homelessness changes over time, how its distribution shifts across places, or how other social issues can be explored through data visualization.



Works Cited

This code through references and cites the following sources:


* U.S. Department of Housing and Urban Development (2024). 2024 AHAR Part 1: PIT Estimates of Homelessness in the U.S. HUD PIT Count Data