library(ggplot2)
library(gcookbook)
library(grid)

Legends

Note: Some problems cannot be completed as written because the code has changed. You will need to find the updated code.

Section 10.1. Removing the Legend

# Figure 10-1
p <- ggplot(PlantGrowth, aes(group, weight, fill = group)) + geom_boxplot()
p

p + guides(fill = FALSE)

Section 10.2. Changing the Position of a Legend

# Figure 10-2
p <- p + scale_fill_brewer(palette = "Pastel2")
p + theme(legend.position = "top")

p + theme(legend.position = c(1,0))

# Figure 10-3
p + theme(legend.position = c(1,0), legend.justification = c(1,0))

p + theme(legend.position = c(1,1), legend.justification = c(1,1))

# Figure 10-4
p + theme(legend.position = c(.85, .2)) +
    theme(legend.background = element_rect(fill = "white", color = "black"))

p + theme(legend.position = c(.85, .2)) +
    theme(legend.background = element_blank()) +
    theme(legend.key = element_blank())

Section 10.3. Changing the Order of Items in a Legend

# Figure 10-5
p <- ggplot(PlantGrowth, aes(group, weight, fill = group)) + geom_boxplot()
p

p + scale_fill_discrete(limits = c("trt1", "trt2", "ctrl"))

# Figure 10-6
p + scale_fill_grey(start = 0.5, end = 1, limits = c("trt1", "trt2", "ctrl"))

p + scale_fill_brewer(palette = "Pastel2", limits = c("trt1", "trt2", "ctrl"))

Section 10.4. Reversing the Order of Items in a Legend

# Figure 10-7
p

p + guides(fill = guide_legend(reverse = TRUE))

Section 10.5. Changing a Legend Title

# Figure 10-8
p

p + labs(fill = "Condition")

# Figure 10-9
hw <- ggplot(heightweight, aes(ageYear, heightIn, color = sex)) +
      geom_point(aes(size = weightLb)) + scale_size_continuous(range = c(1,4))
hw

hw + labs(color = "Male/Female", size = "Weight\n(pounds)")

# Figure 10-10
hw1 <- ggplot(heightweight, aes(ageYear, heightIn, shape = sex, color = sex)) +
      geom_point()
hw1

hw1 + labs(shape = "Male/Female")

hw1 + labs(shape = "Male/Female", color = "Male/Female")

Section 10.6. Changing the Appearance of a Legend Title

# Figure 10-11
p + theme(legend.title = element_text(face = "italic", family = "Times", color = "red", size = 14))

Section 10.7. Removing a Legend Title

# Figure 10-12
p + guides(fill = guide_legend(title = NULL))

Section 10.8. Changing the Labels in a Legend

# Figure 10-13
p + scale_fill_discrete(labels = c("Control", "Treatment 1", "Treatment 2"))

p + scale_fill_grey(start = 0.5, end = 1, 
                    labels = c("Control", "Treatment 1", "Treatment 2"))

# Figure 10-14
p + scale_fill_discrete(limits = c("trt1", "trt2", "ctrl"),
                        labels = c("Treatment 1", "Treatment 2", "Control"))

# Figure 10-15
hw1

hw1 + scale_shape_discrete(labels = c("Female", "Male"))

hw1 + scale_shape_discrete(labels = c("Female", "Male")) +
      scale_color_discrete(labels = c("Female", "Male"))

Section 10.9. Changing the Appearance of Legend Labels

# Figure 10-16
p + theme(legend.text = element_text(face = "italic", family = "Times", color = "red", size = 14))

Section 10.10. Using Labels with Multiple Lines of Text

# Figure 10-17
p + scale_fill_discrete(labels = c("Control", "Type 1\ntreatment",
                                   "Type 2\ntreatment"))

p + scale_fill_discrete(labels = c("Control", "Type 1\ntreatment",
                                   "Type 2\ntreatment")) +
    theme(legend.text = element_text(lineheight = 0.8),
          legend.key.height = unit(1, "cm"))


END!