library(ggplot2)
library(gcookbook)
library(scales)
library(MASS)
library(plyr)

Axes

Section 8.1. Swapping X- and Y- Axes

# Figure 8-1
ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot()

ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot() + coord_flip()

# Figure 8-2
ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot() + coord_flip() +
  scale_x_discrete(limits = rev(levels(PlantGrowth$group)))

Section 8.2. Setting the Range of a Continuous Axis

# Figure 8-3
p <- ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot()
p

p + ylim(0, max(PlantGrowth$weight))

# Figure 8-4
p + scale_y_continuous(limits = c(5, 6.5))

p + coord_cartesian(ylim = c(5, 6.5))

# Figure 8-5
p + expand_limits(y = 0)

Section 8.3. Reversing a Continuous Axis

# Figure 8-6
ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot() + scale_y_reverse()

# Figure 8-7
ggplot(PlantGrowth, aes(group, weight)) + geom_boxplot() + scale_y_reverse(limits = c(8,0))

Section 8.4. Changing the Order of Items on a Categorial Axis

# Figure 8-8
p + scale_x_discrete(limits = c("trt1", "crtl", "trt2"))

p + scale_x_discrete(limits = c("crtl", "trt1"))

# Figure 8-9
p + scale_x_discrete(limits = rev(levels(PlantGrowth$group)))

Section 8.5. Setting the the Scaling Ratio of the X- and Y-Axes

# Figure 8-10
sp <- ggplot(marathon, aes(Half, Full)) + geom_point()
sp + coord_fixed() + 
  scale_y_continuous(breaks = seq(0, 420, 30)) +
  scale_x_continuous(breaks = seq(0, 420, 30))

# Figure 8-11
sp + coord_fixed(ratio = 1/2) + 
  scale_y_continuous(breaks = seq(0, 420, 30)) +
  scale_x_continuous(breaks = seq(0, 420, 15))

Section 8.6. Setting the Position of Tick Marks

# Figure 8-12
p + scale_y_continuous(breaks = c(4, 4.25, 4.5, 5, 6, 8))

# Figure 8-13
p + scale_x_discrete(limits = c("trt2", "ctrl"), breaks = "ctrl")

Section 8.7. Removing Tick Marks and Labels

# Figure 8-14
p + theme(axis.text.y = element_blank())

p + theme(axis.ticks = element_blank(), axis.text.y = element_blank())

p + scale_y_continuous(breaks = NULL)

Section 8.8. Changing the Text of Tick Labels

# Figure 8-15
hwp <- ggplot(heightweight, aes(ageYear, heightIn)) +
  geom_point()
hwp + scale_y_continuous(breaks = c(50, 56, 60, 66, 72),
                         labels = c("Tiny", "Really\nshort", "Short",
                                    "Medium", "Tallish"))

# Figure 8-16
footinch_formatter <- function(x) {
  foot <- floor(x/12)
  inch <- x %% 12
  return(paste(foot, " ' ", inch, "\"", sep = ""))
}
hwp + scale_y_continuous(labels = footinch_formatter)

hwp + scale_y_continuous(breaks = seq(48, 72, 4), labels = footinch_formatter)

Section 8.9. Changing the Appearance of Tick Labels

# Figure 8-17
bp <- p + scale_x_discrete(breaks = c("ctrl", "trt1", "trt2"),
                           labels = c("Control", "Treatment 1", "Treatment 2"))
bp

bp + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

bp + theme(axis.text.x = element_text(angle = 30, hjust = 1, vjust = 1))

# Figure 8-18
bp + theme(axis.text.x = element_text(family = "Times", face = "italic",
                                      color = "darkred", size = rel(0.9)))

Section 8.10. Changing the Text of Axis Labels

# Figure 8-19
hwp <- ggplot(heightweight, aes(ageYear, heightIn, color = sex)) +
  geom_point()
hwp

hwp + xlab("Age in years") + ylab("Height in inches")

# Figure 8-20
hwp + scale_x_continuous(name = "Age\n(years)")

Section 8.11. Removing Axis Labels

# Figure 8-21
p + theme(axis.title.x = element_blank())

Section 8.12. Changing the Appearance of Axis Labels

# Figure 8-22
hwp <- ggplot(heightweight, aes(ageYear, heightIn)) + geom_point()
hwp + theme(axis.title.x = element_text(face = "italic", color = "darkred", size = 14))

# Figure 8-23
hwp + ylab("Height\n(inches)") +
  theme(axis.title.y = element_text(angle = 0, face = "italic", size = 14))

Section 8.13. Showing Lines Along the Axis

# Figure 8-24
hwp + theme(axis.line = element_line(color = "black"))

hwp + theme_bw() +
  theme(panel.border = element_blank(),
        axis.line = element_line(color = "black"))

# Figure 8-25
hwp + theme_bw() +
  theme(panel.border = element_blank(),
        axis.line = element_line(color = "black", size = 4))

hwp + theme_bw() +
  theme(panel.border = element_blank(),
        axis.line = element_line(color = "black", size = 4, lineend = "square"))

Section 8.14. Using a Logarhithmic Axis

# Figure 8-26
p <- ggplot(Animals, aes(body, brain, label = rownames(Animals))) +
  geom_text(size = 3)
p

p + scale_x_log10() + scale_y_log10()

# Figure 8-27
p + scale_x_log10(breaks = 10^(-1:5)) + scale_y_log10(breaks = 10^(0:3))

p + scale_x_log10(breaks = 10^(-1:5),
                  labels = trans_format("log10", math_format(10^.x))) + 
    scale_y_log10(breaks = 10^(0:3),
                  labels = trans_format("log10", math_format(10^.x)))

# Figure 8-28
ggplot(Animals, aes(log10(body), log10(brain), label = rownames(Animals))) +
  geom_text(size = 3)

# Figure 8-29
p + scale_x_continuous(trans = log_trans(),
                       breaks = trans_breaks("log", function(x) exp(x)),
                       labels = trans_format("log", math_format(e^.x))) +
    scale_y_continuous(trans = log2_trans(),
                       breaks = trans_breaks("log2", function(x) 2^x),
                       labels = trans_format("log2", math_format(2^.x)))

# Figure 8-30
ggplot(aapl, aes(date, adj_price)) + geom_line()

ggplot(aapl, aes(date, adj_price)) + geom_line() +
  scale_y_log10(breaks = c(2, 10, 50, 250))

Section 8.15. Adding Ticks for a Logarhithmic Axis

# Figure 8-31
p + annotation_logticks() +
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))

# Figure 8-32
p + annotation_logticks() +
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)),
                minor_breaks = log10(5) + -2:5) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)),
                minor_breaks = log10(5) + -1:3) +
  coord_fixed() +
  theme_bw()

Section 8.16. Making a Circular Graph

# Figure 8-33
ggplot(wind, aes(DirCat, fill = SpeedCat)) +
  geom_histogram(binwidth = 15, origin = -7.5) +
  coord_polar() +
  scale_x_continuous(limits = c(0, 360))

# Figure 8-34
ggplot(wind, aes(DirCat, fill = SpeedCat)) +
  geom_histogram(binwidth = 15, origin = -7.5, color = "black", size = 0.25) +
  guides(fill = guide_legend(reverse = TRUE)) +
  coord_polar() +
  scale_x_continuous(limits = c(0, 360), breaks = seq(0, 360, by = 45),
                     minor_breaks = seq(0, 360, by = 15)) +
  scale_fill_brewer()

# Figure 8-35
md <- data.frame(deaths = as.numeric(mdeaths),
                 month = as.numeric(cycle(mdeaths)))
md <- ddply(md, "month", summarize, deaths = mean(deaths))
p <- ggplot(md, aes(month, deaths)) + geom_line() +
     scale_x_continuous(breaks = 1:12)
p + coord_polar()

p + coord_polar() + ylim(0, max(md$deaths))

# Figure 8-36
p + coord_polar() + ylim(0, max(md$deaths)) + xlim(0, 12)
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.

mdx <- md[md$month == 12, ]
mdx$month <- 0
mdnew <- rbind(mdx, md)
p %+% mdnew + coord_polar() + ylim(0, max(md$deaths))

Section 8.17. Using Dates on an Axis

# Figure 8-37
ggplot(economics, aes(date, psavert)) + geom_line()

# Figure 8-38
econ <- subset(economics, date >= as.Date("1992-05-01") &
                          date < as.Date("1993-06-01"))
p <- ggplot(econ, aes(date, psavert)) + geom_line()
p

datebreaks <- seq(as.Date("1992-06-01"), as.Date("1993-06-01"), by = "2 month")
p + scale_x_date(breaks = datebreaks) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

# Figure 8-39
p + scale_x_date(breaks = datebreaks, labels = date_format("%Y %b")) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

Section 8.18. Using Relative Times on an Axis

# Figure 8.40
www <- data.frame(minute = as.numeric(time(WWWusage)),
                  users  = as.numeric(WWWusage))
timeHM_formatter <- function(x) {
  h <- floor(x/60)
  m <- floor(x %% 60)
  lab <- sprintf("%d:%02d", h, m)
  return(lab)
}
ggplot(www, aes(minute, users)) + geom_line()

ggplot(www, aes(minute, users)) + geom_line() +
  scale_x_continuous(name = "time", breaks = seq(0, 100, by = 10),
                     labels = timeHM_formatter)


END!