.Purpose ggplot2 examples

It has been almost 2 months since I have done any ggplot2 code. Let me engage in deliberate practice.

> library(ggplot2)

> z <- data.frame(x = rep(c("a", "b"), 50))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar()
> print(q)

Play_May3_2010-002.jpg


> z <- data.frame(x = rep(c("a", "b"), 50))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar() + scale_fill_brewer("Set1")
> print(q)

Play_May3_2010-003.jpg


> z <- data.frame(y = rnorm(100), x = rep(c("a", "b"), 50))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar() + scale_fill_brewer("Set1")
> print(q + coord_flip())

Play_May3_2010-004.jpg


> z <- data.frame(y = rnorm(100), x = rep(c(letters[1:10]), 10))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar(fill = "white", colour = "black") + xlab("Vin") +
+     opts(title = "Vinay")
> print(q + scale_y_continuous("AY") + coord_flip())

Play_May3_2010-005.jpg


> z <- data.frame(y = rnorm(100), x = rep(c(letters[1:10]), 10))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar(colour = "black") + xlab("Vin") + opts(title = "Vinay")
> print(q + scale_y_continuous("AY") + coord_flip())

Play_May3_2010-006.jpg


> z <- data.frame(y = rnorm(100), x = rep(c(letters[1:10]), 10))
> p <- ggplot(z, aes(x = x, fill = factor(x)))
> q <- p + geom_bar(colour = "black") + xlab("Vin") + opts(title = "Vinay")
> print(q + scale_y_continuous("AY") + coord_flip())

Play_May3_2010-007.jpg


> p <- ggplot(diamonds, aes(color, fill = cut)) + geom_bar() +
+     coord_flip()
> print(p)

Play_May3_2010-008.jpg


> z <- data.frame(x = rnorm(100), y = rnorm(100))
> p <- ggplot(z, aes(x = x, y = y))
> q <- p + geom_point()
> print(q + geom_abline())

Play_May3_2010-009.jpg


> z <- data.frame(x = rnorm(100), y = rnorm(100))
> p <- ggplot(z, aes(x = x, y = y))
> q <- p + geom_point()
> print(q + geom_abline(intercept = -1, slope = 0.76, colour = "blue",
+     lwd = 3))

Play_May3_2010-010.jpg


> z <- data.frame(x = rnorm(100), y = rnorm(100))
> p <- ggplot(z, aes(x = x, y = y))
> q <- p + geom_point()
> print(q + geom_abline(intercept = -1, slope = 0.76, colour = "blue",
+     size = 1))

Play_May3_2010-011.jpg