library(ggplot2)
library(gcookbook)
library(grid)
Note: Some problems cannot be completed as written because the code has changed. You will need to find the updated code.
# Figure 10-1
p <- ggplot(PlantGrowth, aes(group, weight, fill = group)) + geom_boxplot()
p
p + guides(fill = FALSE)
# 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())
# 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"))
# Figure 10-7
p
p + guides(fill = guide_legend(reverse = TRUE))
# 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")
# Figure 10-11
p + theme(legend.title = element_text(face = "italic", family = "Times", color = "red", size = 14))
# Figure 10-12
p + guides(fill = guide_legend(title = NULL))
# 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"))
# Figure 10-16
p + theme(legend.text = element_text(face = "italic", family = "Times", color = "red", size = 14))
# 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!