library(dplyr)
library(tidyr)
library(highcharter)
my_table <- ftable(UCBAdmissions,row.vars="Dept",col.vars=c("Gender","Admit"))
my_table
seg1_name = "Male"
seg2_name = "Female"
color1 = "#2274c1"
color2 = "#f59000"df <- as.data.frame(my_table) %>%
group_by(Dept, Gender) %>%
summarise(Frequency = sum(Freq)) %>%
ungroup %>%
mutate(Dept = paste("Dept",Dept)) %>%
spread(key = Gender, value = Frequency) %>%
select(Bar_name = Dept, Segment1 = Male, Segment2 = Female)
title_caption = "UC Berkeley applicants by department (1973)"highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = title_caption,
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_plotOptions(column = list(stacking = "normal",
dataLabels = list(enabled = TRUE))) %>%
hc_legend(align = "left", layout = "vertical", verticalAlign = "top",
x=0, y= 250, symbolRadius = 0 ) %>%
hc_xAxis(categories = df$Bar_name,
tickLength = 0, lineWidth = 3, lineColor = "black",
labels = list(style = list(fontSize = "14px", fontWeight = "bold"))) %>%
hc_yAxis(title = list(text = "Number of applicants"),
stackLabels = list(color = "black", fontWeight = "bold", enabled = TRUE)) %>%
hc_tooltip(enabled = FALSE) %>%
hc_add_series(name=seg1_name, data = df$Segment1, color = color1) %>%
hc_add_series(name=seg2_name, data = df$Segment2, color = color2)
title_caption = "Percentage of male versus female applicants by department"
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = title_caption ,
margin = 20, align = "center",
style = list(color = "black", fontWeight = "bold")) %>%
hc_plotOptions(column = list(stacking = "percent",
dataLabels = list(enabled = TRUE, format = "{percentage:,.1f}%"))) %>%
hc_legend(align = "left", layout = "vertical", verticalAlign = "top",
x=0, y= 250, symbolRadius = 0 ) %>%
hc_xAxis(categories = df$Bar_name,
tickLength = 0, lineWidth = 3, lineColor = "black",
labels = list(style = list(fontSize = "14px", fontWeight = "bold"))) %>%
hc_yAxis(title = list(text = "Percentage of applicants"),
labels = list(format = "{value}%")) %>%
hc_tooltip(headerFormat = "<b> {point.key}</b><br>",
pointFormat = "Number of {series.name} applicants: {point.y}<br> Total number: {point.total}<br>Percent of total: {point.percentage:,.2f}%") %>%
hc_add_series(name=seg1_name, data = df$Segment1, color = color1) %>%
hc_add_series(name=seg2_name, data = df$Segment2, color = color2)
top_limit = 2
df <- df %>%
mutate(Total = Segment1 + Segment2) %>%
top_n(top_limit, Total) %>%
arrange(desc(Total))title_caption = paste("The", top_limit, "most popular departments")
# Insert here the code snippet used for the 1st chart

seg1_name = "Admitted"
seg2_name = "Rejected"
color1 = "green"
color2 = "red"df <- as.data.frame(my_table) %>%
group_by(Dept, Admit) %>%
summarise(Frequency = sum(Freq)) %>%
ungroup %>%
mutate(Dept = paste("Dept",Dept)) %>%
spread(key = Admit, value = Frequency) %>%
select(Bar_name = Dept, Segment1 = Admitted, Segment2 = Rejected) %>%
mutate(Total = Segment1 + Segment2) %>%
mutate(Percent_Segment2 = Segment2 / Total) %>%
arrange(desc(Percent_Segment2))title_caption = "Hardest to easiest departments to get into"
# Insert here the code snippet used for the 2nd chart
top_limit = 2
# top
tpn <- df %>%
top_n(top_limit, Percent_Segment2) %>%
mutate(Position = "Hardest") %>%
select(Position, Bar_name)
# bottom
btn <- df %>%
top_n(-top_limit, Percent_Segment2) %>%
mutate(Position = "Easiest") %>%
select(Position, Bar_name)
flt = union(tpn,btn) df <- as.data.frame(my_table) %>%
group_by(Dept, Gender) %>%
summarise(Frequency = sum(Freq)) %>%
ungroup %>%
mutate(Dept = paste("Dept",Dept)) %>%
spread(key = Gender, value = Frequency) %>%
select(Bar_name = Dept, Segment1 = Male, Segment2 = Female) %>%
mutate(Total = Segment1 + Segment2) %>%
mutate(Bar_size = round(Segment2 / Total * 100 ,2)) %>%
inner_join(flt)grouping <- df %>%
group_by(name = Position) %>%
do(categories = .$Bar_name) %>%
list_parse()
color_bar = "#f59000"
title_caption = paste("Women applicants in the ",top_limit, "easiest and hardest departments")hchart(df, "column", hcaes(x = Bar_name, y = Bar_size)) %>%
hc_title(text = title_caption, style = list(fontWeight = "bold")) %>%
hc_plotOptions(column = list(color = color_bar,
dataLabels = list(enabled = TRUE, format = "{y}%"))) %>%
hc_xAxis(categories = grouping, title=list(text = NULL),
tickLength = 0, lineWidth = 3, lineColor = "black",
labels = list(
style = list(fontSize = "14px", fontWeight = "bold"),
groupedOptions = list(list(style = list(fontSize = "18px"))) )) %>%
hc_yAxis(title = list(text = "Percent female applicants"),
labels = list(format = "{value}%"),
allowDecimals = FALSE) %>%
hc_tooltip(enabled = FALSE)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 24 | |
| 14 | |
| 13 | |
| 12 | |
| 11 | |
| 11 | |
| 10 | |
| 9 | |
| 8 | |
| 8 |