In [1]:
library(ggplot2)
library(gcookbook)
library(plyr)


Warning message:
: package ‘ggplot2’ was built under R version 3.3.2

3.1 Basic bar graph


In [ ]:
summary(pg_mean)
str(pg_mean)

In [ ]:
ggplot(data = pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity")

In [ ]:
str(BOD)

In [ ]:
ggplot(data = BOD, aes(x = Time, y = demand)) + geom_bar(stat = "identity")

In [ ]:
ggplot(data = BOD, aes(x = factor(Time), y = demand)) + geom_bar(stat = "identity")

In [ ]:
x <- factor(BOD$Time, ordered = TRUE)
str(x)

In [ ]:
ggplot(data = pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity", fill = "lightblue", colour = "black")

3.2 Grouping bars


In [ ]:
cabbage_exp

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + geom_bar(stat = "identity", position = "dodge")

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar))+ 
geom_bar(stat = "identity", position = "dodge", colour = "black") + scale_fill_brewer(palette = "Pastel1")

3.3 Bar graph of counts


In [ ]:
str(diamonds)

In [ ]:
ggplot(data = diamonds, aes(x = cut)) + geom_bar()

In [ ]:
ggplot(data = diamonds, aes(x = carat)) + geom_bar()

3.4 Using colors in bar graph


In [ ]:
upc <- subset(uspopchange, rank(Change) > 40)
upc

In [ ]:
rank(uspopchange$Change)

In [ ]:
ggplot(data = upc, aes(x = Abb, y = Change, fill = Region)) + geom_bar(stat = "identity")

In [ ]:
ggplot(data = upc, aes(x = reorder(Abb, Change), y = Change, fill = Region)) + 
geom_bar(stat = "identity", colour = "black") +
scale_fill_manual(values = c("#669933", "#FFCC66")) +
xlab("State")

3.5 Coloring positive and negative bar


In [ ]:
csub <- subset(climate, Source == "Berkeley" & Year >= 1900)

In [ ]:
# str(csub)
tail(csub)
csub$pos <- csub$Anomaly10y >= 0

In [ ]:
ggplot(data = csub, aes(x = Year, y = Anomaly10y, fill = pos)) + geom_bar(stat = "identity", position = "identity")

In [ ]:
ggplot(data = csub, aes(x = Year, y = Anomaly10y, fill = pos)) + 
geom_bar(stat = "identity", position = "identity", colour = "black", size = 0.25) +
scale_fill_manual(values = c("#669933", "#FFCC66"), guide = FALSE)

3.6 Adjust bar width and space


In [ ]:
summary(pg_mean)
str(pg_mean)

In [ ]:
ggplot(data = pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity")

In [ ]:
ggplot(data = pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity", width = 1)

In [ ]:
ggplot(data = pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity", width = 0.5)

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.5, position = "dodge")

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))

3.7 Stack bar graph


In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity")

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity") +
guides(fill = guide_legend(reverse = TRUE))

In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar, order = desc(Cultivar))) +
geom_bar(stat = "identity")

In [ ]:
str(cabbage_exp$Cultivar)
cabbage_exp$Cultivar
cultivar = factor(c("c39", "c39","c39", "c52","c52","c52"), levels = c("c52", "c39"))
cultivar
str(cultivar)
identical(cabbage_exp$Cultivar, cultivar)

In [ ]:
ce <- ddply(cabbage_exp, "Date", transform, percentage_weight = Weight / sum(Weight) * 100)
ce

In [ ]:
ggplot(data = ce, aes(x = Date, y = percentage_weight, fill = Cultivar)) +
geom_bar(stat = "identity", colour = "black") +
guides(fill = guide_legend(reverse = FALSE)) +
scale_fill_brewer(palette = "Pastel1")

3.8 Adding labels


In [ ]:
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", position = "dodge", colour = "black") + 
geom_text(aes(y = Weight - 0.1, label = Weight), position = position_dodge(0.9), color = "white")

In [ ]:
ce <- arrange(cabbage_exp, Date, desc(Cultivar))
ce <- ddply(ce, "Date", transform, label_y = cumsum(Weight))
ce

In [ ]:
ggplot(data = ce, aes(x = Date, y = Weight, fill = Cultivar, order=desc(Cultivar))) +
geom_bar(stat = "identity", colour = "black") + 
geom_text(aes(y = label_y - 0.1, label = Weight), color = "white")