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.

Plot25Plot26Plot27 Plot29

Set Aesthetics to Fixed Value

Aesthetics can also be set without calls to the aes() function.

Plot30

Layering Multiple Points Per Observation

Interesting effects can be created by layering points on top of each other.

Plot31 Plot32 Plot33Plot34

Add Regression Lines

Regression lines can be added to the scatterplot to identify overall trends or trends by factor.  Linear models, Loess interpolation or custom models can be added.

Plot35 Plot36 Plot37 Plot38

Managing Over-plotting

It is not uncommon that observations will overlap each other in a scatterplot, making it difficult to see data points and the true distribution.  This is especially likely for large data objects.  geom_jitter() will minimize this problem by “jittering” or moving points slightly to avoid direct overlap.

Plot39 Plot40 Plot41 Plot43

An Example: Fully Loaded

Plot63b

Back | Next

Leave a Reply