ggplot2 part 3

| category RStudy  | tag ggplot2 

Build a plot layer by layer

Layers

library(ggplot2)
data(Oxboys, package = "nlme")
diamonds = na.omit(diamonds)
msleep = na.omit(msleep)
mtcars = na.omit(mtcars)
Oxboys = na.omit(Oxboys)
p <- ggplot(diamonds, aes(carat, price, colour = cut))
# This plot object cannot be displayed until we add a layer
p <- p + layer(geom = "point")
p

plot of chunk ggplot-part3-1


# Here is what a more complicated call looks like.  It produces a histogram
# coloured “steelblue” with a bin width of 2 histogram is a combination of
# bars and binning
p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(geom = "bar", geom_params = list(fill = "steelblue"), stat = "bin",
stat_params = list(binwidth = 2))
p

plot of chunk ggplot-part3-1

# same as the following command
ggplot(diamonds, aes(x = carat)) + geom_histogram(binwidth = 2, fill = "steelblue")

# The following example shows the equivalence between these two ways of
# making plots

ggplot(msleep, aes(sleep_rem/sleep_total, awake)) + geom_point()
# which is equivalent to
qplot(sleep_rem/sleep_total, awake, data = msleep)

# You can add layers to qplot too:
qplot(sleep_rem/sleep_total, awake, data = msleep) + geom_smooth()
# This is equivalent to
qplot(sleep_rem/sleep_total, awake, data = msleep, geom = c("point", "smooth"))
# or
ggplot(msleep, aes(sleep_rem/sleep_total, awake)) + geom_point() + geom_smooth()
# plot objects can be stored as variables. The summary function can be
# helpful for inspecting the structure of a plot without plotting it
p <- ggplot(msleep, aes(sleep_rem/sleep_total, awake))
summary(p)
data: name, genus, vore, order, conservation, sleep_total,
sleep_rem, sleep_cycle, awake, brainwt, bodywt [20x11]
mapping:  x = sleep_rem/sleep_total, y = awake
faceting: facet_null()
p <- p + geom_point()
summary(p)
data: name, genus, vore, order, conservation, sleep_total,
sleep_rem, sleep_cycle, awake, brainwt, bodywt [20x11]
mapping:  x = sleep_rem/sleep_total, y = awake
faceting: facet_null()
-----------------------------------
geom_point: na.rm = FALSE
stat_identity:
position_identity: (width = NULL, height = NULL)

# a set of plots can be initialised using different data then enhanced with
# the same layer

bestfit <- geom_smooth(method = "lm", se = F, colour = "steelblue", alpha = 0.5,
size = 2)

qplot(sleep_rem, sleep_total, data = msleep) + bestfit

plot of chunk ggplot-part3-2

qplot(awake, brainwt, data = msleep, log = "y") + bestfit

plot of chunk ggplot-part3-2

qplot(bodywt, brainwt, data = msleep, log = "xy") + bestfit

plot of chunk ggplot-part3-2

Data

# You can replace the old dataset with %+%
p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p

plot of chunk ggplot-part3-3

mtcars <- transform(mtcars, mpg = mpg^2)
p %+% mtcars

plot of chunk ggplot-part3-3

Aesthetic mappings

Plots and layers
# The **aes** function takes a list of aesthetic-variable pairs aes(x =
# weight, y = height, colour = age)
p <- ggplot(mtcars, aes(x = mpg, y = wt))
p + geom_point()

plot of chunk ggplot-part3-4


# Overriding aesthetics.
p + geom_point(aes(colour = factor(cyl)))

plot of chunk ggplot-part3-4

# overriding y-position (now y is 'disp',although the y lab is still 'wt')
p + geom_point(aes(y = disp))

plot of chunk ggplot-part3-4

Setting vs. mapping
# The difference between setting colour to 'darkblue' and mapping colour to
# 'darkblue'.
p <- ggplot(mtcars, aes(mpg, wt))
p + geom_point(colour = "darkblue")  # setting

plot of chunk ggplot-part3-5

# This sets the point colour to be dark blue instead of black. This is quite
# different than
p + geom_point(aes(colour = "darkblue"))  # mapping

plot of chunk ggplot-part3-5

# qplot
qplot(mpg, wt, data = mtcars, colour = I("darkblue"))  # setting
qplot(mpg, wt, data = mtcars, colour = "darkblue")  # mapping
Grouping
Multiple groups, one aesthetic
# Correctly specifying produces one line per subject.
p <- ggplot(Oxboys, aes(age, height, group = Subject)) + geom_line()
p

plot of chunk ggplot-part3-6

qplot(age, height, data = Oxboys, group = Subject, geom = "line")
# A single line connects all observations.  This pattern is characteristic
# of an **incorrect** grouping aesthetic, and is what we see if the group
# aesthetic is omitted, which in this case is equivalent to group = 1
ggplot(Oxboys, aes(age, height, group = 1)) + geom_line()

plot of chunk ggplot-part3-7

qplot(age, height, data = Oxboys, geom = "line")
Different groups on different layers
# Adding smooths to the Oxboys data. Using the same grouping as the lines
# results in a line of best fit for each boy.
p + geom_smooth(aes(group = Subject), method = "lm", se = F)

plot of chunk ggplot-part3-8

# or
qplot(age, height, data = Oxboys, group = Subject, geom = "line") + geom_smooth(method = "lm",
se = F)
# Using aes(group = 1) in the smooth layer fits a single line of best fit
# across all boys.
p + geom_smooth(aes(group = 1), method = "lm", size = 2, se = F)

plot of chunk ggplot-part3-9

qplot(age, height, data = Oxboys, group = Subject, geom = "line") + geom_smooth(aes(group = 1),
method = "lm", size = 2, se = F)

Further reading


Previous     Next