Getting ggplot2 up and running
If you have this book in your hands, it is very likely you already have a working version of R installed on your computer. If this is not the case, you can download the most up-to-date version of R from the R project website (http://www.r-project.org/). There, you will find a direct connection to the Comprehensive R Archive Network (CRAN), a network of FTP and web servers around the world that store identical, up-to-date versions of code and documentation for R. In addition to access to the CRAN servers, on the website of the R project, you may also find information about R, a few technical manuals, the R journal, and details about the packages developed for R and stored in the CRAN repositories.
At the time of writing, the current version of R is 3.1.2. If you have already installed R on your computer, you can check the actual version with the R.Version()
code, or for a more concise result, you can use the R.version.string
code that recalls only part of the output of the previous function.
Packages in R
In the next few pages of this chapter, we will quickly go through the most important visualization packages available in R, so in order to try the code, you will also need to have additional packages as well as ggplot2
up and running in your R installation. In the basic R installation, you will already have the graphics
package available and loaded in the session; the lattice
package is already available among the standard packages delivered with the basic installation, but it is not loaded by default. ggplot2
, on the other hand, will need to be installed. You can install and load a package with the following code:
> install.packages("ggplot2") > library(ggplot2)
Keep in mind that every time R is started, you will need to load the package you need with the library(name_of_the_package)
command to be able to use the functions contained in the package. In order to get a list of all the packages installed on your computer, you can use the call to the library()
function without arguments. If, on the other hand, you would like to have a list of the packages currently loaded in the workspace, you can use the search()
command. One more function that can turn out to be useful when managing your library of packages is .libPaths()
, which provides you with the location of your R libraries. This function is very useful to trace back the package libraries you are currently using, if any, in addition to the standard library of packages, which on Windows is located by default in a path of the kind C:/Program Files/R/R-3.1.2/library
.
The following list is a short recap of the functions just discussed:
.libPaths() # get library location library() # see all the packages installed search() # see the packages currently loaded