Motivations

The bar plot was inspired by the original bar created by Business Insider:

R Codes

R codes for the chart:

# Clear R environment: 

rm(list = ls())

# Load some R packages: 

library(rvest)
library(magrittr)
library(tidyverse)

# Load admission data for Ivy Colleges: 

link <- "https://www.ivycoach.com/ivy-league-statistics-by-college/"

admissions <- read_html(link) %>% 
  html_table(fill = TRUE, header = FALSE)

# Number of Ivy Colleges: 
n <- length(admissions)

# Function processes data for each college: 

process_college <- function(j) {
  
  admissions[[j]] -> college_j
  
  college_j %>% 
    slice(1) %>%
    as.vector() %>% 
    str_replace_all("[^a-z|A-Z]", "") %>% 
    str_replace_all("University", "") -> col_names
  
  col_names[1] -> Univ_Name
  
  col_names[1] <- "Year"
  
  names(college_j) <- col_names

  college_j %>% 
    slice(-1) %>% 
    mutate(Univ_Name = Univ_Name) -> final_df
  
  return(final_df)
  
}

# Convert data for data frame: 

lapply(1:n, process_college) -> admissions_data

do.call("bind_rows", admissions_data) -> admissions_data

admissions_data %>% 
  mutate(Univ_Name = case_when(Univ_Name == "ofPennsylvania" ~ "Pennsylvania", 
                               Univ_Name == "DartmouthCollege" ~ "Dartmouth", 
                               TRUE ~ Univ_Name)) -> admissions_data

# Data frame for barplot: 

admissions_data %>% 
  filter(Year == "2022") %>% 
  select(Year, TotalAppsReceived, TotalAppsAccepted, Univ_Name) -> df_for_bar

# Function convert to numeric: 

convert_to_num1 <- function(x) {
  
  x %>% 
    str_replace_all(",", "") %>% 
    as.numeric() %>% 
    return()
}

# Conver to numeric and calculate accept rate: 

df_for_bar %>% 
  mutate(Received = convert_to_num1(TotalAppsReceived), 
         Accepted = convert_to_num1(TotalAppsAccepted)) %>% 
  mutate(rate = Accepted / Received) %>% 
  mutate(Rejected = Received - Accepted) %>% 
  arrange(-rate) -> df_for_bar

my_levels <- df_for_bar$Univ_Name

# Convert to long form: 

df_for_bar %>% 
  select(Accepted, Rejected, Univ_Name) %>% 
  pivot_longer(cols = c("Accepted", "Rejected"), values_to = "n", names_to = "type") %>% 
  mutate(Univ_Name = factor(Univ_Name, levels = my_levels)) %>% 
  mutate(type = factor(type, levels = c("Rejected", "Accepted"))) -> df_for_graph1


# Colors for our plot: 

color_cases_text <- "#258BC3"

color_accepted <- "#377eb8"
color_rejected <- "#CE3240" 

bgr_color <- "#EFF2F4"

library(showtext) # -> Package for using extra fonts. 

font_subtitle <- "Roboto Condensed"  # -> Set Outfit font for our plot. 

font_add_google(name = font_subtitle, family = font_subtitle) # -> Load font for using. 

font_text <- "Oswald"

font_add_google(name = font_text, family = font_text)

font_main <- "Ubuntu"  

font_add_google(name = font_main, family = font_main) 

# Automatically render text: 
showtext_auto()

#------------------------
#       Bar Chart
#------------------------

labels_on_x <- str_c(seq(0, 100, 10), "%")

df_for_graph1 %>% 
  filter(type == "Accepted") %>% 
  mutate(text = n, n = 0.04) -> df_text

library(ggtext)

p_title <- "Ivy League Acceptance Rates 2022"

p_caption <- "Source: IVY COACH | Graphic Designer: Nguyen Chi Dung"

p_subtitle <- "Harvard is not just the most selective of the Ivies, but it typically ranks as the most selective university <br> in the United States. Harvard <b style = 'color:#3e6487'> accepted 1962 students </b> or <b style = 'color:#CE3240'> 4.6 percent of the 42749 people </b> who applied<br>in the academic year 2022. Overall, Ivy League Colleges had an average acceptance rate of only 7.1%"
 
df_for_graph1 %>% 
  ggplot(aes(y = Univ_Name, x = n, fill = type)) + 
  geom_col(position = "fill", width = 0.75) + 
  theme_minimal() + 
  theme(legend.position = "top") + 
  theme(axis.title = element_blank()) + 
  theme(panel.grid.minor = element_blank()) + 
  scale_x_continuous(expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = labels_on_x) + 
  labs(title = p_title, subtitle = p_subtitle, caption = p_caption) +   
  theme(plot.subtitle = element_markdown(size = 11, color = "grey20", family = font_subtitle)) +  
  theme(plot.title = element_text(family = font_main, size = 18, face = "bold", color = "grey10")) +  
  theme(plot.caption = element_text(family = font_subtitle, size = 10.5, color = "grey50", vjust = -1)) + 
  theme(plot.margin = unit(c(0.5, 0.8, 0.5, 0.5), "cm")) + 
  theme(axis.text = element_text(family = font_subtitle, size = 12, color = "grey25")) + 
  scale_fill_manual(values = c(Rejected = color_rejected, Accepted = color_accepted)) + 
  theme(panel.grid.major = element_line(color = "grey70")) + 
  theme(legend.title = element_blank()) +  
  theme(legend.text = element_text(size = 11, color = "grey20", family = font_subtitle)) + 
  theme(legend.key.height = unit(0.4, "cm")) + 
  theme(legend.key.width = unit(0.4, "cm")) +  
  guides(fill = guide_legend(reverse = TRUE)) + 
  geom_text(data = df_text, aes(label = text), 
            hjust = 0.97, color = "white", 
            family = font_text, size = 3.5) + 
  theme(plot.background = element_rect(fill = bgr_color, color = NA))