Chapter 4. Line Graphs

library(ggplot2)
library(gcookbook)

Section 4.1. Making a Basic Line Plot

# Figure 4-1
ggplot(BOD, aes(Time, demand)) +
  geom_line()

# Figure 4-2
ggplot(BOD, aes(factor(Time), demand)) +
  geom_line(group=1) +
  xlab("Time")

# Figure 4-3
ggplot(BOD, aes(Time, demand)) +
  geom_line() +
  ylim(0, max(BOD$demand))

Section 4.2. Adding Point to a Line Graph

# Figure 4-4
ggplot(BOD, aes(Time, demand)) +
  geom_line() +
  geom_point()

# Figure 4-5
ggplot(worldpop, aes(Year, Population)) +
  geom_line() +
  geom_point()

ggplot(worldpop, aes(Year, Population)) +
  geom_line() +
  geom_point() +
  scale_y_log10()

Section 4.3. Making a Line Graph with Multiple Lines

# Figure 4-6
library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))
ggplot(tg, aes(dose, length, color = supp)) +
  geom_line()

ggplot(tg, aes(dose, length, linetype = supp)) +
  geom_line()

# Figure 4-7
ggplot(tg, aes(factor(dose), length, color = supp, group = supp)) +
  geom_line()

# Figure 4-8
ggplot(tg, aes(dose, length)) +
  geom_line()

# Figure 4-9
ggplot(tg, aes(dose, length, shape = supp)) +
  geom_line() +
  geom_point(size = 4)

ggplot(tg, aes(dose, length, shape = supp)) +
  geom_line() +
  geom_point(size = 4, shape = 21)

# Figure 4-10
ggplot(tg, aes(dose, length, shape = supp)) +
  geom_line(position = position_dodge(0.2)) +
  geom_point(position = position_dodge(0.2), size = 4)

Section 4.4. Changing the Appearance of Lines

# Figure 4-11
ggplot(BOD, aes(Time, demand)) +
  geom_line(linetype = "dashed", size = 1, color = "blue")

# Figure 4-12
ggplot(tg, aes(dose, length, color = supp)) +
  geom_line() +
  scale_color_brewer(palette = "Set1")

# Figure 4-13
ggplot(tg, aes(dose, length, group = supp)) +
  geom_line(color = "darkgreen", size =1.5)

ggplot(tg, aes(dose, length, color = supp)) +
  geom_line(linetype="dashed") +
  geom_point(shape=22, size=3, fill="white")

Section 4.5. Changing the Appearance of Points

# Figure 4-14
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_line() +
  geom_point(size=4, shape=22, color='darkred', fill='pink')

# Figure 4-15
ggplot(BOD, aes(x=Time, y=demand)) +
  geom_line() +
  geom_point(size=4, shapae=21, fill='white')
## Warning: Ignoring unknown parameters: shapae

# Figure 4-16
pd <-  position_dodge(0.2)
ggplot(tg, aes(x=dose, y=length, fill=supp)) + 
  geom_line(position=pd) + 
  geom_point(shape=21, size=3, position=pd) + 
  scale_fill_manual(values=c("black", "white"))

Section 4.6. Making a Graph with Shaded Areas

# Figure 4-17
sunspotyear = data.frame(
  Year = as.numeric(time(sunspot.year)),
  Sunspots = as.numeric(sunspot.year))

ggplot(sunspotyear, aes(x=Year, y=Sunspots)) + geom_area()

# Figure 4-18
ggplot(sunspotyear, aes(x=Year, y=Sunspots)) +
  geom_area(color='black', fill='blue', alpha=.2)

# Figure 4-19
ggplot(sunspotyear, aes(x=Year, y=Sunspots)) +
  geom_area(fill='blue', alpha=.2) +
  geom_line()

Section 4.7. Making a Stacked Area Graph

# Figure 4-20
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + geom_area()

# Figure 4-21
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) +
  geom_area(color = 'black', size=.2, alpha=.4) + 
  scale_fill_brewer(palette='Blues', breaks=rev(levels(uspopage$AgeGroup)))

# Figure 4-22
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup, order=desc(AgeGroup))) + 
  geom_area(color='black', size=.2, alpha=.4) +
  scale_fill_brewer(palette='Blues')

# Figure 4-23
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup, order=desc(AgeGroup))) +
  geom_area(color=NA, alpha=.4) +
  scale_fill_brewer(palette='Blues') +
  geom_line(position='stack', size=.2)

Section 4.8. Making a Proportional Stacked Area Graph

# Figure 4-24
uspopage_prop <- ddply(uspopage, "Year", transform,
                       Percent = Thousands / sum(Thousands) * 100)
ggplot(uspopage_prop, aes(Year, Percent, fill = AgeGroup)) +
  geom_area(color = 'black', size=.2, alpha=.4) + 
  scale_fill_brewer(palette='Blues', breaks=rev(levels(uspopage$AgeGroup)))

Section 4.9. Adding a Confidence Region

# Figure 4-25
clim <- subset(climate, Source == "Berkeley",
               select=c("Year", "Anomaly10y", "Unc10y"))
ggplot(clim, aes(Year, Anomaly10y)) +
  geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y+Unc10y), alpha=0.2) +
  geom_line()

# Figure 4-26
ggplot(clim, aes(Year, Anomaly10y)) +
  geom_line(aes(y=Anomaly10y-Unc10y), color = "grey50", linetype = "dotted") +
  geom_line(aes(y=Anomaly10y+Unc10y), color = "grey50", linetype = "dotted") +
  geom_line()


END!