csv file with header

Here we have the same data, but now the file is comma-delimited and contains a header. We can import this file with

students = read.table("data/students.tsv", header = T, sep = ",")

By setting header = T, we tell R that the first line of the file contains column names, so we can immediately access students$age and so on. (Note: there is also a read.csv function that uses sep = "," by default.)

help

There are many more options that read.table can take. For a full list of these, just type help(read.table) (or equivalently, ?read.table) at the prompt to access documentation.

This works for other functions as well.

ggplot2

With these R basics in place, let’s dive into the ggplot2 package.

Installation

One of R’s greatest strengths is its excellent set of packages. To install a package, you can use the install.packages() function.

install.packages("ggplot2")

To load a package into your current R session, use library().

library(ggplot2)

Scatterplots with qplot()

Let’s look at how to create a scatterplot in ggplot2. We’ll use the iris data frame that’s automatically loaded into R.

What does the data frame contain? We can use the head function to look at the first few rows.

# But we can also supply a different weight.
# Here the height of each bar is the total running time of the director's movies.
qplot(director, weight = minutes, data = movies, geom = "bar", ylab = "total length (min.)")