How to Install and Load ggplot2 in R

By Andrie de Vries, Joris Meys

Because ggplot2 isn’t part of the standard distribution of R, you have to download the package from CRAN and install it.

The Comprehensive R Archive Network (CRAN) is a network of servers around the world that contain the source code, documentation, and add-on packages for R.

Each submitted package on CRAN also has a page that describes what the package is about. You can view the ggplot2 page for more information.

Although it’s fairly common practice to simply refer to the package as ggplot, it is, in fact, the second implementation of the grammar of graphics for R; hence, the package is ggplot2. As of this writing, the current version of the package is version 0.9.0.

Perhaps somewhat confusingly, the most important function in this package is ggplot(). Notice that the function doesn’t have a 2 in its name. So, be careful to include the 2 when you install.packages() or library() the package in your R code, but the function ggplot() itself does not contain a 2.

Here is how to install a package for the first time with the install.packages() function and to load the package at the start of each R session with the library() function.

To install the ggplot2 package, use the following:

> install.packages("ggplot2")

And then to load it, use the following:

> library("ggplot2")