Install and load ggplot

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

# load libraries 
library(ggplot2)



Download and Load Data

download.file("http://www.openintro.org/stat/data/evals.RData", destfile = "evals.RData")
load("evals.RData")



Create Histogram Plot Function

# this function will create a histogram of every variable in your data frame
# the function takes one argument ('data') which is the name of your data frame 
# if you want to save the scatter plots as .png files define the file path for graphs_folder below 
# you will also need to remove the "#" from the two lines in the function that are commented out and place a "#" before print

# save graphs in this folder
graphs_folder <- '/filepath/graphs/'

hist <- function(x, na.rm = TRUE, ...) {
  nm <- names(x)
  for (i in seq_along(nm)) {
    print(ggplot(x,aes_string(x = nm[i])) + geom_histogram(alpha=.8, fill = "darkblue") + theme_classic()) }
    #plots <- ggplot(x,aes_string(x = nm[i])) + geom_histogram(alpha=.8, fill = "darkblue") + theme_classic() 
    #ggsave(plots,filename=paste(graphs_folder, "hist_",nm[i],".png",sep=""))}
}



Run Histogram Plot Function to Create Plots for an Entire Data Frame

hist(evals)

plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4