Category Archives: ggplot2
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
# Plot skeleton
p <- ggplot(mtcars, aes(wt, mpg))
# Plot1: basic scatter plot of car mpg vs. weight
p + geom_point()
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.
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.
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:
main = "Anderson's Iris Data",
pch = 21,
bg = c("red", "green2", "steelblue4")[unclass(iris$Species)])