Install Packagaes

#install.packages("dslabs")
library("dslabs")
data(package="dslabs")
list.files(system.file("script", package = "dslabs"))
##  [1] "make-admissions.R"                   
##  [2] "make-brca.R"                         
##  [3] "make-brexit_polls.R"                 
##  [4] "make-death_prob.R"                   
##  [5] "make-divorce_margarine.R"            
##  [6] "make-gapminder-rdas.R"               
##  [7] "make-greenhouse_gases.R"             
##  [8] "make-historic_co2.R"                 
##  [9] "make-mnist_27.R"                     
## [10] "make-movielens.R"                    
## [11] "make-murders-rda.R"                  
## [12] "make-na_example-rda.R"               
## [13] "make-nyc_regents_scores.R"           
## [14] "make-olive.R"                        
## [15] "make-outlier_example.R"              
## [16] "make-polls_2008.R"                   
## [17] "make-polls_us_election_2016.R"       
## [18] "make-reported_heights-rda.R"         
## [19] "make-research_funding_rates.R"       
## [20] "make-stars.R"                        
## [21] "make-temp_carbon.R"                  
## [22] "make-tissue-gene-expression.R"       
## [23] "make-trump_tweets.R"                 
## [24] "make-weekly_us_contagious_diseases.R"
## [25] "save-gapminder-example-csv.R"

Select Admissions Dataset

data("admissions")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.7     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Load Theme and Color Libraries

library(ggthemes)
library(ggrepel)
library(RColorBrewer)

Structure Admissions Dataset

str(admissions)
## 'data.frame':    12 obs. of  4 variables:
##  $ major     : chr  "A" "B" "C" "D" ...
##  $ gender    : chr  "men" "men" "men" "men" ...
##  $ admitted  : num  62 63 37 33 28 6 82 68 34 35 ...
##  $ applicants: num  825 560 325 417 191 373 108 25 593 375 ...

Install Highcharter

#install.packages("highcharter")
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## 
## Attaching package: 'highcharter'
## The following object is masked from 'package:dslabs':
## 
##     stars

Number of Admitted Students by Major and Gender

highchart() %>% 
  hc_add_series(data=admissions,
                type = "area",
                hcaes(x=major, y = admitted, group = gender)) %>%
  hc_plotOptions(series = list(stacking="normal")) %>%
  hc_xAxis(categories=admissions$major, title= list(text="Major")) %>% 
  hc_yAxis(title = list(text = "Number of Admitted Applicants")) %>%
  hc_legend(align="right", verticalAlign="bottom") %>%
  hc_colors (brewer.pal(7, "Set2")) %>%
  hc_title(text = "Number of Admitted Students by Major and Gender")

Number of Admitted Students by Major and Gender

Using a Different Stacking Option

highchart() %>% 
  hc_add_series(data=admissions,
                type = "area",
                hcaes(x=major, y = admitted, group = gender)) %>%
  hc_plotOptions(series = list(stacking="percent")) %>%
  hc_xAxis(categories=admissions$major, title= list(text="Major")) %>% 
  hc_yAxis(title = list(text = "Number of Admitted Applicants")) %>%
  hc_legend(align="right", verticalAlign="bottom") %>%
  hc_colors (brewer.pal(4, "Set1")) %>%
  hc_title(text = "Number of Admitted Students by Major and Gender")

Using the admissions dataset, these graphs show the number of admitted students by major and gender. The first graph is displayed as a normal stack and the second graph is displayed using a percentage stack.