library(ggplot2)
library(gcookbook)
# Figure 3-1
ggplot(pg_mean, aes(group, weight)) +
geom_bar(stat="identity")
# Figure 3-2
ggplot(BOD, aes(Time, demand)) +
geom_bar(stat = "identity")
ggplot(BOD, aes(factor(Time), demand)) +
geom_bar(stat = "identity")
# Figure 3-3
ggplot(pg_mean, aes(group, weight)) +
geom_bar(stat="identity", fill = "lightblue", color = "black")
# Figure 3-4
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat="identity")
# Figure 3-5
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", color = "black", stat = "identity") +
scale_fill_brewer(palette="Pastel1")
# Figure 3-6
ce <- cabbage_exp[1:5, ]
ggplot(ce, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", color = "black", stat = "identity") +
scale_fill_brewer(palette="Pastel1")
# Figure 3-7
ggplot(diamonds, aes(cut)) +
geom_bar()
# Figure 3-8
ggplot(diamonds, aes(carat)) +
geom_bar()
upc <- subset(uspopchange, rank(Change)>40)
# Figure 3-9
ggplot(upc, aes(Abb, Change, fill = Region)) +
geom_bar(stat = "identity")
# Figure 3-10
ggplot(upc, aes(reorder(Abb, Change), Change, fill = Region)) +
geom_bar(stat="identity", color = "black") +
scale_fill_manual(values=c("#669933", "#FFCC66")) +
xlab("State")
csub <- subset(climate, Source=="Berkeley" & Year >= 1900)
csub$pos <- csub$Anomaly10y >= 0
# Figure 3-11
ggplot(csub, aes(Year, Anomaly10y, fill = pos)) +
geom_bar(stat="identity", position = "identity")
# Figure 3-12
ggplot(csub, aes(Year, Anomaly10y, fill=pos)) +
geom_bar(stat="identity", position = "identity", color = "black", size = 0.25) +
scale_fill_manual(values=c("#CCEEFF", "#FFDDDD"), guide=FALSE)
# Figure 3-13
ggplot(pg_mean, aes(group, weight)) +
geom_bar(stat="identity")
ggplot(pg_mean, aes(group, weight)) +
geom_bar(stat="identity", width = 0.5)
ggplot(pg_mean, aes(group, weight)) +
geom_bar(stat="identity", width=1)
#Figure 3-14
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.5, position = "dodge")
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))
# Figure 3-15
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.9, position = position_dodge(0.9))
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.2, position = position_dodge(0.9))
# Figure 3-16
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity")
# Figure 3-17
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity") +
guides(fill=guide_legend(reverse=TRUE))
# Figure 3-18
library(plyr)
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar, order = desc(Cultivar))) +
geom_bar(stat="identity")
# Figure 3-19
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity", color="black") +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_brewer(palette="Pastel1")
ce <- ddply(cabbage_exp, "Date", transform,
percent_weight = Weight / sum(Weight) * 100)
# Figure 3-20
ggplot(ce, aes(Date, percent_weight, fill = Cultivar)) +
geom_bar(stat="identity")
# Figure 3-21
ggplot(ce, aes(Date, percent_weight, fill = Cultivar)) +
geom_bar(stat="identity", color="black") +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_brewer(palette="Pastel1")
# Figure 3-22
ggplot(cabbage_exp, aes(interaction(Date, Cultivar), Weight)) +
geom_bar(stat="identity") +
geom_text(aes(label=Weight), vjust=1.5, color="white")
ggplot(cabbage_exp, aes(interaction(Date, Cultivar), Weight)) +
geom_bar(stat="identity") +
geom_text(aes(label=Weight), vjust=-0.2)
# Figure 3-23
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=Weight), vjust=1.5, color="white",
position=position_dodge(.9), size=3)
ce <- arrange(cabbage_exp, Date, Cultivar)
ce <- ddply(ce, "Date", transform, label_y=cumsum(Weight))
# Figure 3-24
ggplot(ce, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity") +
geom_text(aes(y=label_y, label=Weight), vjust=1.5, color="white")
# Figure 3-25
ce <- arrange(cabbage_exp, Date, Cultivar)
ce <- ddply(ce, "Date", transform, label_y=cumsum(Weight)-0.5*Weight)
ggplot(ce, aes(Date, Weight, fill = Cultivar)) +
geom_bar(stat="identity") +
geom_text(aes(y=label_y, label=Weight), color="white")
# Figure 3-27
tophit <- tophitters2001[1:25, ]
ggplot(tophit, aes(avg, name)) +
geom_point()
# Figure 3-28
ggplot(tophit, aes(avg, reorder(name, avg))) +
geom_point(size = 3) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x= element_blank(),
panel.grid.major.y = element_line(color = "grey60", linetype="dashed"))
# Figure 3-29
ggplot(tophit, aes(reorder(name, avg), avg)) +
geom_point(size = 3) +
theme_bw() +
theme(axis.text.x = element_text(angle=60, hjust=1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x= element_blank(),
panel.grid.major.y = element_line(color = "grey60", linetype="dashed"))
# Figure 3-30
nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
tophit$name <- factor(tophit$name, levels=nameorder)
ggplot(tophit, aes(avg, name)) +
geom_segment(aes(yend=name), xend=0, color="grey50") +
geom_point(size=3, aes(color=lg)) +
scale_color_brewer(palette="Set1", limit=c("NL", "AL")) +
theme_bw() +
theme(panel.grid.major.y = element_blank(),
legend.position = c(1, 0.55),
legend.justification = c(1, 0.5))
# Figure 3-31
ggplot(tophit, aes(avg, name)) +
geom_segment(aes(yend=name), xend=0, color="grey50") +
geom_point(size=3, aes(color=lg)) +
scale_color_brewer(palette="Set1", limit=c("NL", "AL"), guide=FALSE) +
theme_bw() +
theme(panel.grid.major.y = element_blank()) +
facet_grid(lg ~ ., scales = "free_y", space= "free_y")
END!