Create Data Frame of Majors and FTE by Department at Reed College

Departments = c('Art' , 'Music', 'Theatre', 'Anthropology', 'Economics',
                'History', 'Political Science', 'Sociology', 'Chinese',  'Classics',
                'English', 'French', 'German', 'Russian', 'Spanish',
                'Biology', 'Chemistry', 'Mathematics', 'Physics', 'Linguistics',
                'Philosophy', 'Psychology', 'Religion')

Majors = c(58, 21, 16, 52, 56,
          57, 68, 28, 6, 20, 
          150, 5, 2, 7, 3, 
          153.5, 74, 72.5, 125, 45,
          75, 98, 25)

FTE = c(7.8, 4, 6.25, 5, 5.6, 
        8.7, 5.5, 3, 3, 4, 
        12, 5, 3, 3, 5, 
        9, 6.8, 8, 6, 4, 
        5.7, 7.7, 4)

data <- data.frame(Departments, Majors, FTE)

# Data does not include 94 interdisciplinary majors and 40 undecided majors.  
# Majors like bio/chem are split between the two departments 
# General Lit majors are included with English 
# Dance majors and faculty are included with Theatre
# Major Data: http://www.reed.edu/ir/ir_internal_web/intendedmajors.html and FTE Data: http://www.reed.edu/ir/facfte.html



Create Histogram using Base R Commands

hist(data$Majors)

plot of chunk unnamed-chunk-2



Add Additional Elements to Base Histogram

hist(data$Majors,
     xlab = "Number of Majors", ylab = "Frequency",  main = "Histogram of Majors", pch = 16, # Add labels
     breaks=12,  # set number of bins
     col = "dark blue", lwd = 2) # change color and width of line

plot of chunk unnamed-chunk-3



Create Kernal Density using Base R Commands

plot(density(data$Majors), xlim = c(0, 200))

plot of chunk unnamed-chunk-4



Add Additional Elements to Base Density Plot

plot(density(data$Majors),
     xlim = c(0, 200),
     xlab = "Number of Majors", ylab = "Density",  main = "Histogram of Majors", pch = 16, # Add labels
     col = "dark blue", lwd = 4) # change color and width of line

plot of chunk unnamed-chunk-5



Using ggplot2 to Make a Histogram

# This demo requires the 'ggplot' package 
if( !is.element("ggplot2", installed.packages()[,1]) )
  install.packages("ggplot2")

suppressPackageStartupMessages(library(ggplot2))

## Base histogram plot in ggplot 
ggplot(data, aes(x=Majors)) + geom_histogram()

plot of chunk unnamed-chunk-6



Apply Theme to Histogram Plot

ggplot(data, aes(x=Majors)) + 
  geom_histogram() +
  theme_classic()

plot of chunk unnamed-chunk-7



Add Additional Elements to Histogram

ggplot(data, aes(x=Majors)) + 
  geom_histogram(color="dark blue", size=1, fill="light blue", binwidth=15) +  # change color and adjust bindwidth
  ggtitle("Histogram of Reed College Majors") + # add a title to the plot 
  theme_classic()

plot of chunk unnamed-chunk-8



Using ggplot to Make a Density Plot

ggplot(data, aes(x=Majors)) + 
  geom_density(color="dark blue", size=1, fill="light blue") + # change to geom_density for density plot 
  ggtitle("Kernal Density of Reed College Majors") + 
  theme_classic()

plot of chunk unnamed-chunk-9



Make Your Histogram Interactive with googleVis

## This demo requires the 'googleVis' package 
if( !is.element("googleVis", installed.packages()[,1]) )
  install.packages("googleVis")

suppressPackageStartupMessages(library(googleVis))


# make a new data frame with only columns to plot 
keep <- c('Departments', 'Majors')
data2 <- data[keep]

# create interactive histogram plot using googleVis
Hist <- gvisHistogram(data2, options=list(
  legend="{ position: 'right', maxLines: 2 }",
  colors="['#1A8763']",
  width=750, height=500))
# plot interactive scatter (use 'plot(Hist)' to view in RStudio)
print(Hist, 'chart')