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:
<- "https://www.ivycoach.com/ivy-league-statistics-by-college/"
link
<- read_html(link) %>%
admissions html_table(fill = TRUE, header = FALSE)
# Number of Ivy Colleges:
<- length(admissions)
n
# Function processes data for each college:
<- function(j) {
process_college
-> college_j
admissions[[j]]
%>%
college_j slice(1) %>%
as.vector() %>%
str_replace_all("[^a-z|A-Z]", "") %>%
str_replace_all("University", "") -> col_names
1] -> Univ_Name
col_names[
1] <- "Year"
col_names[
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",
== "DartmouthCollege" ~ "Dartmouth",
Univ_Name 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:
<- function(x) {
convert_to_num1
%>%
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
<- df_for_bar$Univ_Name
my_levels
# 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:
<- "#258BC3"
color_cases_text
<- "#377eb8"
color_accepted <- "#CE3240"
color_rejected
<- "#EFF2F4"
bgr_color
library(showtext) # -> Package for using extra fonts.
<- "Roboto Condensed" # -> Set Outfit font for our plot.
font_subtitle
font_add_google(name = font_subtitle, family = font_subtitle) # -> Load font for using.
<- "Oswald"
font_text
font_add_google(name = font_text, family = font_text)
<- "Ubuntu"
font_main
font_add_google(name = font_main, family = font_main)
# Automatically render text:
showtext_auto()
#------------------------
# Bar Chart
#------------------------
<- str_c(seq(0, 100, 10), "%")
labels_on_x
%>%
df_for_graph1 filter(type == "Accepted") %>%
mutate(text = n, n = 0.04) -> df_text
library(ggtext)
<- "Ivy League Acceptance Rates 2022"
p_title
<- "Source: IVY COACH | Graphic Designer: Nguyen Chi Dung"
p_caption
<- "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%"
p_subtitle
%>%
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))