2) Creating a Scatter Plot for Closer Inspection with ggplot2.

This Blog entry is from the Linear Regression section in Learn R.

The scatter plot matrix created beforehand is an extremely useful and informative tool, if lacking beauty.  A package that cannot escape mention for the creation of graphics in R is ggplot2, which is a powerful and flexible graphics package for creating charts and visualisations every bit as beautiful as that which could be found in Excel. 

Start by installing the ggplot package using RStudio and as described in procedure 9:

1.png

Clicking install to download and install the package:

2.png

Once the packages has been downloaded and installed, reference the package using the library() function and its name ggplot2:

library(ggplot2)
3.png

Run the line of script to console:

4.png

In this example a scatter plot will be created with the Dependent Vector on the y Axis and the Median_4 on the x axis, and initially using just the built in function plot():

plot(FDX$Median_4,FDX$Dependent)
5.png

The signature of the plot() function is effortless and it is a fantastic extensions to perform quick and exploratory data analysis,  although it may not be visually impressive enough for the purposes of presentations.  qplot() is a function in the ggplot2 package and achieves much the same,  just visually more striking:

qplot(FDX$Median_4,FDX$Dependent)
6.png

Run the line of script to console:

7.png

The package ggplot2 provides a plethora of functions that will create rich and visually impressive graphics, from the being able to manipulate colours to correctly titling a plot with the intention of creating graphics fit for publishing. 

The ggplot functionality will be steadily introduced in subsequent Blog entries although creating visually striking charts for publication is outside the scope of this course.

11) List all Functions in a Package.

This Blog entry is from the Getting Started in R section in Learn R.

Once a package is loaded, beyond using the help, an understanding of all the functions available to the package can be obtained.   Although a script active, console passive approach is advocated this Blog entry is one of the few occasions where it is more appropriate to use the console directly rather than clutter up the script.

Click on the console window in the bottom left hand corner of RStudio:

typing-directly-into-the-r-console.png

Type directly into the console:

ls("package:ggplot2")
ls-ready-to-be-written-out-in-the-r-console.png

Press the Enter key to execute the script:

all-the-functions-in-the-ggplot2-package-written-to-the-r-console.png

A list of all functions in the package is returned.

10) Load Packages using Script.

This Blog entry is from the Getting Started in R section in Learn R.

While the toggle function is a useful feature of RStudio,  the intention is to maintain a script Active, Console Passive approach, henceforth it is important to ensure that the librarby() function call, to be streamed to the console is moved to the head of the script and that the detach() function is moved to the base of the script.

Start by navigating to the very top of the script and create a new line in the script editor. Navigate to the start of the first line and press the enter key:

adding-library-to-top-of-r-script.png

Invoking the library() function,  type:

library(ggplot2)
loading-ggplot2-using-library-function-in-r.png

Intelisense will look through the search path and suggest some packages,  and this can also be auto-completed to ggplot2.  Upon completion of the line,  run the script line to the console:

r-console-packag-loaded-ggplot2.png

The ggplot library is now loaded as the first line of the script. 

9) Load and Unload Packages in RStudio.

This Blog entry is from the Getting Started in R section in Learn R.

When a package is installed it is not by default available for use, to save memory and resources.  Loading a package in RStudio is an extremely simple toggle process which will send the command to console to load a specific package on select,  unload on deselect.

Loading a package uses the library() function, invoked before a script is run.

Navigate to the packages pane in the bottom right hand corner of the RStudio, clicking on the tab if necessary.

ggplot2-in-the-rstudio-package-window.png

Note the package that was installed previously,  ggplot2, and the check box to the left hand side of the package name.  To load the package simply select the textbox via a click of the mouse:

toggle-loading-a-package-in-rstudio-r-console.png

On selection of the checkbox the library() function, complete with the required parameters, will be processed in the console.  It can also be observed that the location of the package has been specified in this function call,  although that is not strictly necessary.

To unload the package,  deselect the checkbox in the packages pane next to ggplot2:

toggle-unloading-package-in-rstudio-r-console.png

On deselection of the checkbox the detatch() function is sent to the console for the package (notice the string will match the return of the search() function).

8) Review Help and Documentation.

This Blog entry is from the Getting Started in R section in Learn R.

In the packages pane a list of all packages installed has been presented. Having followed previous Blog entries we know that ggplot2 is now installed.  It is customary for packages to carry good documentation,  which can be accessed by clicking on the hyperlink overlaying the package name:

using-rstudio-to-navigate-to-help-via-link.png
ggplot2-link-in-rstudio.png

Clicking on the link immediate navigation to the packages documentation takes place:

package-help-in-r-being-displayed-in-rstudio.png

This feature provides a more intuitive means to navigate to documentation for functions. In this example,  scroll down and clock on the function link autoplot:

r-ggplot2-function-help.png

It can be seen that the documentation is displayed, navigating away from the index.