1 Creating a plot

library(ggplot2)

data("diamonds")

p <- ggplot(data = diamonds, mapping = aes(x = carat, y = price, color = cut))

2 Layers

p <- p + layer(geom = "point")

p
p <- ggplot(data = diamonds, mapping = aes(x = carat))

p <- p + layer(geom = "histogram", geom_params = list(fill = "steelblue"), stat = "bin", stat_params = list(binwidth = 0.2))

p
p <- ggplot(data = diamonds, mapping = aes(x = carat))

p <- p + geom_histogram(binwidth = 0.2, fill = "steelblue") 

p
data("msleep")

str(msleep)
## 'data.frame':    83 obs. of  11 variables:
##  $ name        : chr  "Cheetah" "Owl monkey" "Mountain beaver" "Greater short-tailed shrew" ...
##  $ genus       : chr  "Acinonyx" "Aotus" "Aplodontia" "Blarina" ...
##  $ vore        : Factor w/ 4 levels "carni","herbi",..: 1 4 2 4 2 2 1 NA 1 2 ...
##  $ order       : chr  "Carnivora" "Primates" "Rodentia" "Soricomorpha" ...
##  $ conservation: Factor w/ 7 levels "","cd","domesticated",..: 5 NA 6 5 3 NA 7 NA 3 5 ...
##  $ sleep_total : num  12.1 17 14.4 14.9 4 14.4 8.7 7 10.1 3 ...
##  $ sleep_rem   : num  NA 1.8 2.4 2.3 0.7 2.2 1.4 NA 2.9 NA ...
##  $ sleep_cycle : num  NA NA NA 0.133 0.667 ...
##  $ awake       : num  11.9 7 9.6 9.1 20 9.6 15.3 17 13.9 21 ...
##  $ brainwt     : num  NA 0.0155 NA 0.00029 0.423 NA NA NA 0.07 0.0982 ...
##  $ bodywt      : num  50 0.48 1.35 0.019 600 ...
ggplot(data = msleep, mapping = aes(x = sleep_rem / sleep_total, y = awake)) + geom_point()
## Warning: Removed 22 rows containing missing values (geom_point).
qplot(x = sleep_rem / sleep_total, y = awake, data = msleep)
## Warning: Removed 22 rows containing missing values (geom_point).
ggplot(data = msleep, mapping = aes(x = sleep_rem / sleep_total, y = awake)) + geom_point() + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).
qplot(x = sleep_rem / sleep_total, y = awake, data = msleep, 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.
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).
p <- ggplot(data = msleep, mapping = aes(x = sleep_rem / sleep_total, y = awake)) + geom_point(na.rm = TRUE) + geom_smooth()

summary(p)
## data: name, genus, vore, order, conservation, sleep_total,
##   sleep_rem, sleep_cycle, awake, brainwt, bodywt [83x11]
## mapping:  x = sleep_rem/sleep_total, y = awake
## faceting: facet_null() 
## -----------------------------------
## geom_point: na.rm = TRUE 
## stat_identity:  
## position_identity: (width = NULL, height = NULL)
## 
## geom_smooth:  
## stat_smooth:  
## position_identity: (width = NULL, height = NULL)

3 Data

data("mtcars")

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
p <- ggplot(data = mtcars, mapping = aes(x = mpg, y = wt, color = factor(cyl))) + geom_point()

p
mtcars <- transform(mtcars, mpg = mpg ** 2)

p %+% mtcars
rm(list = ls())

4 Aesthetic Mappings

4.1 Plots and Layers

data(mtcars)

p <- ggplot(mtcars)

summary(p)
## data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
##   [32x11]
## faceting: facet_null()
p <- p + aes(x = wt, y = hp)

summary(p)
## data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
##   [32x11]
## mapping:  x = wt, y = hp
## faceting: facet_null()
p + geom_point(aes(color = factor(cyl))) + geom_smooth(method = "lm", se = FALSE)

4.2 Setting vs Mapping

p <- ggplot(data = mtcars, mapping = aes(x = wt, y = hp))

q <- ggplot(data = mtcars, mapping = aes(x = wt, y = hp))


# Setting
p + geom_point(color = "red")
qplot(x = wt, y = hp, data = mtcars, color = I("darkblue"))
# Mapping
q + geom_point(aes(color = cyl))
q + geom_point(aes(color = "red"))
qplot(x = wt, y = hp, data = mtcars, color = "darkblue")
rm(list = ls())

4.3 Grouping

library(nlme)

data("Oxboys")

str(Oxboys)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   234 obs. of  4 variables:
##  $ Subject : Ord.factor w/ 26 levels "10"<"26"<"25"<..: 13 13 13 13 13 13 13 13 13 5 ...
##  $ age     : num  -1 -0.7479 -0.463 -0.1643 -0.0027 ...
##  $ height  : num  140 143 145 147 148 ...
##  $ Occasion: Ord.factor w/ 9 levels "1"<"2"<"3"<"4"<..: 1 2 3 4 5 6 7 8 9 1 ...
##  - attr(*, "formula")=Class 'formula' length 3 height ~ age | Subject
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ y: chr "Height"
##   ..$ x: chr "Centered age"
##  - attr(*, "units")=List of 1
##   ..$ y: chr "(cm)"
##  - attr(*, "FUN")=function (x)  
##   ..- attr(*, "source")= chr "function (x) max(x, na.rm = TRUE)"
##  - attr(*, "order.groups")= logi TRUE
p <- ggplot(data = Oxboys, mapping = aes(x = age, y = height)) + geom_line()

p
p <- ggplot(data = Oxboys, mapping = aes(x = age, y = height, group = Subject)) + geom_line()

p
ggplot(data = Oxboys, mapping = aes(x = age, y = height, group = Subject)) + geom_line() + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
ggplot(data = Oxboys, mapping = aes(x = age, y = height, group = Subject)) + geom_line() + geom_smooth(aes(group = 1), method = "lm", se = FALSE, size = 2)

4.4 Overriding the default grouping

ggplot(data = Oxboys, mapping = aes(x = Occasion, y = height)) + geom_boxplot()
ggplot(data = Oxboys, mapping = aes(x = Occasion, y = height, group = Subject)) + geom_boxplot()
ggplot(data = Oxboys, mapping = aes(x = Occasion, y = height, group = 1)) + geom_boxplot()
p <- ggplot(data = Oxboys, mapping = aes(x = Occasion, y = height)) + geom_boxplot()

library(ggthemes)

p + geom_line(mapping = aes(group = Subject), color = "steelblue") + theme_economist()
p + geom_line(mapping = aes(group = Subject), color = "steelblue") + geom_smooth(method = "lm", color = "red", size = 3, mapping = aes(group = 1)) + theme_wsj()

5 Few more examples

d <- ggplot(data = diamonds, mapping = aes(x = carat)) + xlim(c(0, 3))

d + stat_bin(mapping = aes(ymax = ..count..), binwidth = 0.1, geom = "area")
d + stat_bin(mapping = aes(size = ..density..), binwidth = 0.1, geom = "point", position = "identity")
## Warning: Removed 2 rows containing missing values (geom_point).