closure_data <- read.csv("Closure-cases AN FY24.csv")
referral_data <- read.csv("Referral-cases AN FY24.csv")

As I initially uploaded an un-cleaned data-set, I then went back to the original excel files to clean the data.

I began by deleting all columns except “Age” and “Gender” to avoid having to clean other miscellaneous data. I then went through my “Age” and “Gender” data removing inconsistent data including negative ages, blank gender boxes, and genders listed as “unknown.” I repeated this twice for referral cases and closure cases.

Once completed I re-uploaded the now cleaned datasets.

#"Gender" data and column was going straight into ggplot and it wasn't categorizing the gender data when assigned a color. This is because it was not added as a factor and thus wasn't categorizing the gender data so I had to convert it to "as.factor. 

referral_data$Gender <- as.factor(referral_data$Gender)


#I had to define a smaller-sized bin width for more clarity when reading the graph. I started off with 10 (ages grouped by 10 years), which I found too large and made the labels and thus data confusing to read, I went down to 5 then 3 and still wasn't satisfied until I reached 2.

bin_width <- 2  


#This was added after the first few runs of my graph as I was having trouble with my x-axis labels. I wanted to clearly indicate that each bar covered the span of two years to clearly read the data. Initially the boxes just read 15, 17, 19 etc...
#I set up age_breaks and age_labels and searched up and found how to define the age ranges so that each label represented an age range (15-16, 17-18, etc). This made sure that the bars were between the ticks for these age ranges to represent the age range clearly and started from the smallest age in the data set and stopped at the max, largest age. 

age_breaks <- seq(floor(min(referral_data$Age)), ceiling(max(referral_data$Age)), by=bin_width)
age_labels <- paste(age_breaks, age_breaks + bin_width - 1, sep="-")


#My data contained a wide range of ages and my question was interested in patterns or trends across the spectrum of ages in my data, so I treated age as continuous variable and chose to use a histogram. 

hist_plot <- ggplot(referral_data, aes(x=Age, fill=Gender)) + 


#I had to add the breaks() feature as my bin_width was not aligned with the age breaks I wanted. This allowed me to set the break points I wanted on my axis.
#I also had to search up and use the closed() feature as my graph was miscounting my age distribution since it was not including the upper age in each range.
#I was also having trouble with my bars overlapping on my graph making it hard to tell where the data for each age range began. I added the "position="dodge" parameter in geom_histogram() so that the bars for different genders appeared side by side within each age range instead of overlapping.

   geom_histogram(breaks=age_breaks, binwidth=bin_width, closed="left", position="dodge") +
  
  
#I wanted to add new colors to my histogram, specifically teal and maroon. I tried typing these colors normally, however, the colors weren't recognized so I searched for the appropriate color codes. 

  scale_fill_manual(values=c("#008080", "#800000")) +
  
#I was having trouble with the layout of the x-axis. The breaks between the ranges were not clear so the bars all blended together. To correct this I edited my scale_x_continuous(), setting locations for the ticks to the breaks. I labeled by the age ranges I defined in age_breaks and age_labels so each bin represents an age range (e.g., 15-16, 17-18). I specified age ranges 15-16 instead of just 15 for clarity even though my bin width has been set to 2.

  scale_x_continuous(breaks=age_breaks, labels=age_labels) +


#I also had to use trial and error to adjust my y-axis for case count as the scale was initially showing fractional values, and then not going up until the max case count in my data. 
# To fix this I used the max_hist argument to find the highest case count in the age groups and set that as the max value on my y-scale. This fixed my fractional scale however, it did not fix that the scale did not go high enough. I scrapped that and instead chose to use limits () so that the y-axis only displayed values from 0 to 10. 
#I then struggled with empty space above and beneath the y-axis and all the plotted bars so I searched and used expand() to specify  that the axis would start at 0 and end at 10.

  scale_y_continuous(
    breaks=0:10,  # Whole number breaks from 0 to 10
    limits = c(0, 10),  # Set y-axis upper limit to 10
    expand = c(0, 0)
  ) +

#Labeling axis and title accordingly 

  labs(x="Age at time of Referral", y="Number of Cases", fill="Gender") +
  ggtitle("Age and Gender Distribution of Referral Cases") +
  
  
#I was having trouble with the background and finishing touches of my graph. My grid lines were extending beyond my graph (no bound),  my grid lines were inconsistent, and I wanted to add a border to clean the final graph. I found the theme() function through a youtube video to do this.

theme_minimal() +


#For organization I searched up arguments I could use in theme to tilt my x-axis labels and center my graph at the half-way mark. I titled my x-axis labels by 45 degrees as they would not fit horizontally by using axis.text.x. I then wanted to center my graph title and used plot.title as I wanted the title to be placed halfway (0.5).

  theme(
    axis.text.x = element_text(angle=45, hjust=1, vjust=1),
    plot.title = element_text(hjust = 0.5),
    
    
#I searched and found the "panel" argument to change the color of all necessary grid lines to grey and removed unnecessary grid lines as they made interpreting the age ranges of my data confusing with panel.grid.minor.

    panel.grid.major = element_line(color="grey80"),
    panel.grid.minor = element_blank(),
    

#I added a black border around my entire finalized graph and made sure the ticks were black for better readability.

    panel.border = element_rect(colour = "black", fill=NA, size=1),
    axis.ticks = element_line(color = "black")
  )
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
#Print the final histogram
print(hist_plot) 

The previous code has been repeated for the closure cases dataset

library(ggplot2)

closure_data$Gender <- as.factor(closure_data$Gender)

bin_width <- 2

age_breaks <- seq(floor(min(closure_data$Age)), ceiling(max(closure_data$Age)), by=bin_width)
age_labels <- paste(age_breaks, age_breaks + bin_width - 1, sep="-")

hist_plot <- ggplot(closure_data, aes(x=Age, fill=Gender)) + 
  geom_histogram(breaks=age_breaks, binwidth=bin_width, closed="left", position="dodge") +
  scale_fill_manual(values=c("#008080", "#800000")) +
  scale_x_continuous(breaks=age_breaks, labels=age_labels) +
  scale_y_continuous(
    breaks=0:10,  # Whole number breaks from 0 to 10
    limits = c(0, 10),  # Set y-axis upper limit to 10
    expand = c(0, 0)
  ) +
  
  labs(x="Age at time of Closure", y="Number of Cases", fill="Gender") +
  ggtitle("Age and Gender Distribution of Closure Cases") +
  
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle=45, hjust=1, vjust=1),
    plot.title = element_text(hjust = 0.5),
    panel.grid.major = element_line(color="grey80"),
    panel.grid.minor = element_blank(),
    panel.border = element_rect(colour = "black", fill=NA, size=1),
    axis.ticks = element_line(color = "black")
  )

print(hist_plot)