1 Introduction

library(ggplot2)

?qplot

data("diamonds")

set.seed(1410)

dsmall <- diamonds[sample(1:nrow(diamonds), size = 100),]

qplot(x = carat, y = price, data = diamonds)
qplot(x = log(carat), y = log(price), data = diamonds)
qplot(x = carat, y = x * y * z, data = diamonds)

2 Color, Size, Shape and other aesthetic attributes

qplot(x = carat, y = price, data = dsmall, color = color)
qplot(x = carat, y = price, data = dsmall, shape = cut)
qplot(x = carat, y = price, data = diamonds, alpha = I(1/10))
qplot(x = carat, y = price, data = diamonds, alpha = I(1/100))

3 Plot geoms

3.1 Adding a smoother to a plot

qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
qplot(x = carat, y = price, data = diamonds, geom = c("point", "smooth"), alpha = I(1/5))
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
qplot(x = carat, y = price, data = diamonds, geom = c("point", "smooth"), alpha = I(1/5), se = FALSE)
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess")
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess", span = 0.2, se = FALSE)
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "loess", span = 1, se = FALSE)
library(mgcv)

qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x))
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x, bs = "cs"))
library(splines)

qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm")
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula = y ~ poly(x, 3))
qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula = y ~ ns(x, 4))
library(MASS)

qplot(x = carat, y = price, data = dsmall, geom = c("point", "smooth"), method = "rlm")

3.2 Boxplots and Jittered plots

qplot(x = color, y = price / carat, data = diamonds, geom = "jitter")
qplot(x = color, y = price / carat, data = diamonds, geom = "jitter", alpha = I(1/10))
qplot(x = color, y = price / carat, data = diamonds, geom = "boxplot")

3.3 Histogram and Density Plots

qplot(x = carat, data = diamonds, geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.1)
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.2)
qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.2, fill = color)
qplot(x = carat, data = diamonds, geom = "density")
qplot(x = carat, data = diamonds, geom = "density", adjust = 0.5, xlim = c(0, 3))
## Warning: Removed 32 rows containing non-finite values (stat_density).
qplot(x = carat, data = diamonds, geom = "density", color = color, fill = color, alpha = I(1/5))

3.4 Bar Chart

qplot(x = color, data = diamonds, geom = "bar")
qplot(x = color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat")

3.5 Time Series with Line and Path Plots

data("economics")

str(economics)
## 'data.frame':    478 obs. of  6 variables:
##  $ date    : Date, format: "1967-06-30" "1967-07-31" ...
##  $ pce     : num  508 511 517 513 518 ...
##  $ pop     : int  198712 198911 199113 199311 199498 199657 199808 199920 200056 200208 ...
##  $ psavert : num  9.8 9.8 9 9.8 9.7 9.4 9 9.5 8.9 9.6 ...
##  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: int  2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...
qplot(x = date, y = unemploy / pop, data = economics, geom = "line")
qplot(x = date, y = uempmed, data = economics, geom = "line")
year1900 <- function(x) {
    return(as.POSIXlt(x)$year + 1900)
}

qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"))
qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"), color = year1900(date))
qplot(x = unemploy / pop, y = uempmed, data = economics, geom = c("point", "path"), color = year1900(date)) + scale_size_area()

4 Faceting

qplot(x = carat, data = diamonds, geom = "histogram", binwidth = 0.1, xlim = c(0, 3), facets = color ~ .)
qplot(x = carat, ..density.., data = diamonds, geom = "histogram", binwidth = 0.1, xlim = c(0, 3), facets = color ~ .)

5 qplot options

qplot(x = carat, y = price, data = dsmall, xlab = "Price ($)", ylab = "Weight (carats)", main = "Price-Weight Relationship")
qplot(x = carat, y = price / carat, data = dsmall, ylab = expression(frac(price, carat)), xlab = "Weight (carats)", main = "Small Diamonds", xlim = c(0.2, 1))
## Warning: Removed 35 rows containing missing values (geom_point).
qplot(x = carat, y = price, data = dsmall, log = "xy")
rm(list = ls())