Category Archives: ggplot2

Boxplots (ggplot)


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


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 | Comments Off on Boxplots (ggplot)

Error Bars (ggplot)

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


Synthetic sensor data set is created.

View the data:

Basic Error Bar Syntax


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 | Comments Off on Error Bars (ggplot)

Facetting (ggplot)

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


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 | Comments Off on Facetting (ggplot)

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 | Comments Off on Colors in ggplot

Legends (ggplot)

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


The diamonds data that ships with ggplot.

The Default Legend

The following example presents the default legend to be cusotmized.






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 | Comments Off on Legends (ggplot)

Line Graphs (ggplot)

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


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


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 | Comments Off on Line Graphs (ggplot)

Multiple Plots (ggplot)

The multiplot() Function

Winston Chang’s R Graphical Cookbook provides a useful function to simplify the creation of layouts with multiple plots.  The function accepts ggplot objects as inputs.

Use of the function is straightforward.  First, multiplot() needs to be sourced and available in memory.  Then the plots need to be coded with variable assignments to create plot objects.  Finally, multiplot() is used to call the plot objects for placement in the predefined plot layout.

Posted in ggplot2, R Graphics | Comments Off on Multiple Plots (ggplot)

Other Geoms (ggplot)

All geoms that ship with ggplot2 are listed for below.  See HTML help in R for detailed argument structures and examples.

Posted in ggplot2 | Comments Off on Other Geoms (ggplot)

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 | Comments Off on R Graphics (ggplot2)

Scatter Plots (ggplot)

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


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



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 | Comments Off on Scatter Plots (ggplot)

Text (ggplot)



Back | Next

Posted in ggplot2 | Comments Off on Text (ggplot)

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 | Comments Off on The Grammar of Graphics

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 | Comments Off on Themes (ggplot)

Titles (ggplot)

Options to control titles in ggplot are described below.

Chapter Content

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


The diamonds dataset that ships with ggplot.

Basic Plot: No Titles


Title Aesthetics

Plot80 Plot81 Plot82 Plot83

Back | Next

Posted in ggplot2 | Comments Off on Titles (ggplot)

Axes (ggplot)


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 | Comments Off on Axes (ggplot)