library(ggplot2)
library(gcookbook)
library(plyr)
library(grid)
# Figure 7.1
p <- ggplot(faithful, aes(eruptions, waiting)) + geom_point()
p + annotate("text", x = 3, y = 48, label = "Group 1") +
annotate("text", x = 4.5, y = 66, label = "Group 2")
# Figure 7.2
p + annotate("text", x = 3, y = 48, label = "Group 1", family = "serif",
fontface = "italic", color = "darkred", size = 3) +
annotate("text", x = 4.5, y = 66, label = "Group 2", family = "serif",
fontface = "italic", color = "darkred", size = 3)
# Figure 7.3
p + annotate("text", x = 3, y = 48, label = "Group 1", alpha = 0.1) +
geom_text(x = 4.5, y = 66, label = "Group 2", alpha = 0.1)
# Figure 7.4
p + annotate("text", x = -Inf, y = Inf, label = "Upper left", hjust = -0.2, vjust = 2) +
annotate("text", x = mean(range(faithful$eruptions)), y = -Inf, vjust = -0.4, label = "Bottom middle")
# Figure 7.5
p <- ggplot(data.frame(x = c(-3, 3)), aes(x = x)) + stat_function(fun = dnorm)
p + annotate("text", x = 2, y = 0.3, parse = TRUE,
label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
# Figure 7.6
p + annotate("text", x = 0, y = 0.05, parse = TRUE, size = 4,
label =" 'Function: ' * y == frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
# Figure 7.7
p <- ggplot(heightweight, aes(x = ageYear, y = heightIn, color = sex)) + geom_point()
p + geom_hline(yintercept = 60) + geom_vline(xintercept = 14)
p + geom_abline(intercept = 37.4, slope = 1.75)
# Figure 7.8
hw_means <- ddply(heightweight, "sex", summarise, heightIn = mean(heightIn))
p + geom_hline(aes(yintercept = heightIn, color = sex), data = hw_means,
linetype = "dashed", size = 1)
# Figure 7.9
pg <- ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_point()
pg + geom_vline(xintercept = 2)
pg + geom_vline(xintercept = which(levels(PlantGrowth$group) == "ctrl"))
# Figure 7.10
p <- ggplot(subset(climate, Source == "Berkeley"), aes(Year, Anomaly10y)) + geom_line()
p + annotate("segment", x = 1950, xend = 1980, y = -0.25, yend = -0.25)
# Figure 7.11
p + annotate("segment", x = 1850, xend = 1820, y = -0.8, yend = -0.95, color = "blue",
size = 2, arrow = arrow()) +
annotate("segment", x = 1950, xend = 1980, y = -0.25, yend = -0.25,
arrow = arrow(ends = "both", angle = 90, length = unit(.2, "cm")))
# Figure 7.12
p <- ggplot(subset(climate, Source == "Berkeley"), aes(Year, Anomaly10y)) + geom_line()
p + annotate("rect", xmin = 1950, xmax = 1980, ymin = -1, ymax = 1, alpha = 0.1, fill = "blue")
# Figure 7.13
ggplot(PlantGrowth, aes(group, weight, fill = group)) + geom_boxplot() +
scale_fill_manual(values = c("grey85", "grey85", "#FFDDCC"), guide = FALSE)
# Figure 7.14
ce <- subset(cabbage_exp, Cultivar == "c39")
ggplot(ce, aes(x = Date, y = Weight)) +
geom_bar(fill = "white", color = "black", stat = "identity") +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), width = 0.2)
ggplot(ce, aes(Date, Weight)) +
geom_line(aes(group = 1)) +
geom_point(size = 4)
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), width = 0.2)
## mapping: ymin = Weight - se, ymax = Weight + se
## geom_errorbar: na.rm = FALSE, width = 0.2
## stat_identity: na.rm = FALSE
## position_identity
# Figure 7.15
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), position = "dodge", width = 0.2)
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se), position = position_dodge(0.9), width = 0.2)
# Figure 7.16
pd <- position_dodge(0.3)
ggplot(cabbage_exp, aes(Date, Weight, color = Cultivar, group = Cultivar)) +
geom_errorbar(aes(ymin = Weight - se, ymax = Weight + se),
width = 0.2, size = 0.25, color = "black", position = pd) +
geom_line(position = pd) +
geom_point(position = pd, size = 2.5)
# Figure 7.17
p <- ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(. ~ drv)
f_labels <- data.frame(drv = c("4", "f", "r"), label = c("4wd", "Front", "Rear"))
p + geom_text(x = 6, y = 40, aes(label = label), data = f_labels)
p + annotate("text", x = 6, y = 42, label = "label text")
# Figure 7.18
lm_labels <- function(dat) {
mod <- lm(hwy ~ displ, data = dat)
formula <- sprintf("italic(y) == %.2f %+.2f * italic(x)",
round(coef(mod)[1], 2), round(coef(mod)[2], 2))
r <- cor(dat$displ, dat$hwy)
r2 <- sprintf("italic(R^2) == %.2f", r^2)
data.frame(formula = formula, r2 = r2, stringAsFactors = FALSE)
}
labels <- ddply(mpg, "drv", lm_labels)
p + geom_smooth(method = lm, se = FALSE) +
geom_text(x = 3, y = 40, aes(label = formula), data = labels, parse = TRUE, hjust = 0) +
geom_text(x = 3, y = 35, aes(label = r2), data = labels, parse = TRUE, hjust = 0)
END!