# 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)

## Scatter Plots (ggplot)

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

*Data*

*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*

*Basic Scatter Plot Syntax*

1 2 3 4 5 6 |
library(ggplot2) # Plot skeleton p <- ggplot(mtcars, aes(wt, mpg)) # Plot1: basic scatter plot of car mpg vs. weight p + geom_point() |

*Aesthetics*

*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.

*(204 words, 20 images, estimated 49 secs reading time)*

## Plotting Forecast Data Objects Using ggplot

Robert Hyndman is the author of the forecast package in R. I’ve been using the package for long-term time series forecasts. The package comes with some built in methods for plotting forecast data objects in R that Ive wanted to customize for improved clarity and presentation. The following article achieves that goal and shares two scripts for plotting forecast data objects using ggplot.

*(498 words, 2 images, estimated 2:0 mins reading time)*

## Correlation Plots in R

The standard function for correlation plots in R is pairs(), which generates a matrix of scatter plots based on all pairwise combinations of variables in a data object. The standard graph looks something like this after a little color enhancement:” *Click to enlarge*

The code behind this plot is simple:

1 2 3 4 |
pairs(iris[1:4], main = "Anderson's Iris Data", pch = 21, bg = c("red", "green2", "steelblue4")[unclass(iris$Species)]) |

*(163 words, 2 images, estimated 39 secs reading time)*