Category Archives: ggplot2

R Graphics (ggplot2)

The flexibility of R graphics using the package ggplot is illustrated through a series of structured examples.  

The Grammar of Graphics
Building Layered Plots
Scatter Plots (ggplot)
Bar Graphs (ggplot)
Line Graphs (ggplot)
Boxplots (ggplot)
Error Bars (ggplot)
Facetting (ggplot)
Titles (ggplot)
Axes (ggplot)
Legends (ggplot)
Other Geoms (ggplot)
Multiple Plots (ggplot)
Themes (ggplot)

Back | Next

Posted in ggplot2 | Leave a comment

Scatter Plots (ggplot)

Scatter plots in ggplot are simple to construct and can utilize many format options.

Data

The mtcars data frame ships with R and was extracted from the 1974 US Magazine Motor Trend.  The data compares fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). 

Basic Scatter Plot Syntax

Plot24

Aesthetics

Scatter plot aesthetics are used to control selected x and y data, color (by name),  point shape (1 thru 24), alpha level (or transparency), point size, and point fill.

Posted in ggplot2 | Leave a comment

Text (ggplot)

 

 

Back | Next

Posted in ggplot2 | Leave a comment

The Grammar of Graphics

Comparison of Graphic Tools

When it comes to producing graphics in R, there are basically three choices:

  • Base graphics which ships with R,
  • the lattice package extension, and
  • the ggplot2 package extension.

Base graphics was described extensively in the previous few chapters, and is the preferred choice for creating highly customized charts, like the polar windrose plot below, where flexibility and control over all graph objects is essential:

Posted in ggplot2 | Leave a comment

Themes (ggplot)

Quick Intro

Themes and theme elements control the non-data components of the plot. The use, of predefined themes and custom themes in ggplot are described below.  

ggplot provides two built-in format themes.  theme_grey() is the default theme and has a grey plot background.  theme_bw() is a simple black and white theme.  ggplot also provides functions to modify theme elements or to create new themes.

Posted in ggplot2 | Leave a comment

Titles (ggplot)

Options to control titles in ggplot are described below.

Chapter Content

  • Data
  • Basic Plot: No Title Aesthetics
  • Title Aesthetics

Data

The diamonds dataset that ships with ggplot.

Basic Plot: No Titles

Plot78

Title Aesthetics

Plot80 Plot81 Plot82 Plot83

Back | Next

Posted in ggplot2 | Leave a comment

Axes (ggplot)

Data

A simple sequence of numbers is provided to populate the base graph.

Plot84aClick to enlarge

Axes Text Formats

The following examples modify axes text formats with colors and bold font so they are easier to see.  The second chart benefits from axis text being rotated 90 degrees.

Plot91 Plot92

Posted in ggplot2 | Leave a comment

Building Layered Plots

Layered plots are one way to achieve new insight and actionable intelligence when working with complex data.  ggplot is well suited for layered plots.

Data Pre-Processing

To make graphs with ggplot(), the data must be in a data frame and in “long” (as opposed to wide) format.  Converting between “wide” and “long” data formats is facilitated with the reshape2 package.  Specifically, the melt() function converts wide to long format, and the cast() function converts long to wide format.  The following code block presents examples of the two data formats.

Posted in ggplot2, R Graphics, R Programming | Leave a comment

Bar Graphs (ggplot)

Data

The mtcars data frame ships with R and was extracted from the 1974 US Magazine Motor Trend.  The data compares fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). 

Basic Bar Graph Syntax

A basic plot skeleton is created by declaring the data, aesthetic mappings.  The additional stat argument is set equal to “bin” (the default) to obtain counts of the data observations and “identity” and to plot observed values.

Plot44

Posted in ggplot2 | Leave a comment

Boxplots (ggplot)

Data

diamonds is a dataset that ships with ggplot2 with observations from almost 54,000 diamonds.  Data slicing is possible by price, carat, cut, color, clarity, size, depth and table width. Boxplots are ideally suited for visualizing data variability.

Basic Boxplot Syntax

Plot60b

Boxplot Aesthetics

Boxplot aesthetics define the x input data and have several argument parameters that control box attributes: ymin,lower quartile,  middle bar statistic, upper quartile, and ymax.  Additional arguments include color, fill, linetype, shape, size, weight and alpha transparency level.

Posted in ggplot2 | Leave a comment

Error Bars (ggplot)

Error bars are a basic chart type in ggplot and easy to execute.

Data

Synthetic sensor data set is created.

View the data:

Basic Error Bar Syntax

Plot65

Error Bar Aesthetics

geom_errorbar() takes the following aesthetic arguments: x axis data, ymin and ymax value data, color, linetype, horizontal bar width, and the alpha or transparency level.  Error bars can be plotted alone or in layers with points, line and bars, as shown below. 

Posted in ggplot2 | Leave a comment

Facetting (ggplot)

Facetting is a process that combines data sub-setting and data visualization.

Data

The diamonds dataset that ships with ggplot.

Plot Syntax: facet_grid

Facets are multiple small plots, each representing a slice of data.  Facetting is a powerful data analysis and exploration tool.  facet_grid produces a lattice grid of plot panels with basic row and column identifiers.  

Plot70 Plot71 Plot72 Plot73

Posted in ggplot2 | Leave a comment

Colors in ggplot

There are number of ways to control the default colors in ggplot.

The HCL Color Wheel

ggplot simplifies color choice with its default color selection, which are based on a “color wheel.”  The result is a well balanced graphic that doesn’t draw too much attention to any one color.  ggplot uses the HCL color wheel and the hue_pal() function from the scales package.  Specifically, if there are two colors, then they will be selected from opposite points on the circle; if there are three colors, they will be 120° apart on the color circle; and so on.  This ensure that discrete data has maximum contrast as a function of the number variables present.

Posted in ggplot2, R Colors | Leave a comment

Legends (ggplot)

Legends are a key component of data visualization.  ggplot format controls are defined below.

Data

The diamonds data that ships with ggplot.

The Default Legend

The following example presents the default legend to be cusotmized.

Legend1

 

 

 

 

Click to enlarge

Removing the Legend

Legends are created for different aesthetics, such as fill, color/colour, linetype, shape, etc. Each aesthetic has a scale function than can be called to remove the legend:

Posted in ggplot2 | Leave a comment

Line Graphs (ggplot)

ggplot line graph example are provided to illustrate different format options.

Data

The Orange data frame ships with R and depicts the growth of fruit trees as a function of age (days).  The data object has 35 rows and 3 columns in long data format.

Basic Line Plot Syntax

Plot57

Line Plot Aesthetics

Line chart aesthetics control selected x and y data, color (by name), linetype and size, transparency or alpha level.  Line symbols or points are added using geom_point().

Posted in ggplot2 | Leave a comment