Purpose This will give a nice summary of all the geoms that are available in ggplot2 package.

Let me first prepare the dataset

> library(RSQLite)
> library(ggplot2)
> date.start <- "2009-02-02"
> date.end <- "2010-02-18"
> db <- "C:/sqlite/mydbases/pairs/pairsv1.s3db"
> drv <- dbDriver("SQLite")
> con <- dbConnect(drv, dbname = db)
> query <- paste(" REMOVED DELIBERATELY by ticker_id, trade_date asc ",
+     sep = "")
> security.db <- dbGetQuery(con, query)
> query <- " select * from security_master"
> master <- dbGetQuery(con, query)
> dataset <- merge(security.db, master, by.x = "ticker", by.y = "ticker",
+     all.x = T)
> dataset <- dataset[, -5]
> colnames(dataset) <- c("ticker", "ticker.id", "trade.date", "price",
+     "market.cap", "sector", "sector.id")
> colnames(master) <- c("ticker.id", "ticker", "market.cap", "sector.name",
+     "sector.id")
> master$mcap.rank <- factor(round(rank(master$market.cap)/38) +
+     1)
> tickers <- unique(master$ticker)
> security.db <- security.db[, 3:4]
> security.db1 <- unstack(security.db, form = security.db$price ~
+     security.db$ticker)
> y.t <- "UNIONBANK"
> x.t <- "PNB"
> dates <- as.Date(unique(dataset$trade.date))


geom_abline

> library(ggplot2)
> temp <- as.data.frame(cbind(security.db1[, y.t], security.db1[,
+     x.t]))
> colnames(temp) <- c(y.t, x.t)
> temp2 <- temp
> beta <- coef(lm(temp[, 1] ~ temp[, 2]))
> p <- ggplot(temp, aes(y = UNIONBANK, x = PNB)) + geom_point()
> q <- p + geom_abline(intercept = beta[1], slope = beta[2], colour = "sienna",
+     size = 1.5)
> print(q)

geoms-002.jpg



geom_bar

  • Simple Bar chart
> p <- ggplot(master, aes(x = sector.name)) + geom_bar() + coord_flip()
> print(p)

geoms-003.jpg

  • Subplot with market category range
> p <- ggplot(master, aes(x = sector.name, fill = mcap.rank))
> q <- p + geom_bar() + coord_flip()
> print(q)

geoms-004.jpg

  • Using dodge
> q <- p + geom_bar(position = "dodge") + coord_flip()
> print(q)

geoms-005.jpg

  • Using frequency polygon
> p <- ggplot(master, aes(x = sector.name, colour = mcap.rank,
+     fill = mcap.rank))
> q <- p + geom_freqpoly(aes(group = mcap.rank)) + coord_flip()
> print(q)

geoms-006.jpg




geom_area

> dim(master)
[1] 136   6
> temp <- data.frame(id = 1:136, market.cap = master$market.cap)
> head(temp)
  id market.cap
1  1       4174
2  2      15503
3  3      13125
4  4       1106
5  5      12924
6  6       5340
> p <- ggplot(temp, aes(x = id, y = market.cap))
> q <- p + geom_area()
> print(q)

geoms-007.jpg



geom_blank

  • Nothing to see here
> p <- qplot(mcap.rank, market.cap, data = master, geom = "blank")
> print(p)

geoms-008.jpg



geom_boxplot

  • A simple Boxplot
> p <- ggplot(master, aes(as.factor(mcap.rank), market.cap/5000))
> q <- p + geom_boxplot()
> print(q)

geoms-009.jpg

  • Boxplot + Jitter
> q <- p + geom_boxplot() + geom_jitter()
> print(q)

geoms-010.jpg

  • Boxplot + Jitter + Outlier
> q <- p + geom_boxplot(outlier.colour = "green", outlier.size = 3) +
+     geom_jitter()
> print(q)

geoms-011.jpg

  • Boxplot + Jitter + Fill
> q <- p + geom_boxplot(aes(fill = mcap.rank)) + geom_jitter()
> print(q)

geoms-012.jpg

  • Boxplot + Jitter + Fill + Flip
> q <- p + geom_boxplot(aes(fill = mcap.rank)) + geom_jitter()
> q <- q + coord_flip()
> print(q)

geoms-013.jpg

> head(master)
  ticker.id     ticker market.cap        sector.name sector.id mcap.rank
1         1       ABAN       4174 Oil Field Services        17         2
2         2        ABB      15503          Utilities        25         3
3         3        ACC      13125             Cement         4         3
4         4 ADLABSFILM       1106              Media        14         1
5         5  AMBUJACEM      12924             Cement         4         3
6         6 ANDHRABANK       5340            PSUBank         3         2
> q <- qplot("Pairs Universe", market.cap, data = master, geom = "boxplot")
> print(q)

geoms-014.jpg



geom_histogram

> temp <- as.data.frame(cbind(security.db1[, y.t], security.db1[,
+     x.t]))
> colnames(temp) <- c(y.t, x.t)
> p <- ggplot(temp, aes(x = PNB))
> q <- p + geom_histogram()
> print(q)
> q <- p + geom_histogram(binwidth = 10)
> print(q)
> q <- p + geom_histogram(binwidth = 1)
> print(q)

geoms-015.jpg

  • Plot density
> p <- ggplot(temp2, aes(x = PNB))
> q <- p + geom_density()
> print(q)
> x1 <- c(rnorm(1000, 2, 1), rnorm(1000, 4, 2), rnorm(1000, 5,
+     0.4), rnorm(1000, 1, 2))
> type <- c(rep("a", 1000), rep("b", 1000), rep("c", 1000), rep("d",
+     1000))
> temp <- data.frame(counts = x1, type = type)
> p <- ggplot(temp, aes(x = counts, fill = type)) + xlab("") +
+     ylab("")
> q <- p + geom_histogram()
> print(q)
> q <- p + geom_histogram() + xlim(-4, 5)
> print(q)
> q <- p + geom_density()
> print(q)
> p <- ggplot(temp, aes(x = counts)) + xlab("") + ylab("")
> q <- p + geom_histogram(aes(fill = type))
> print(q)
> p <- ggplot(temp, aes(x = counts)) + xlab("") + ylab("")
> q <- p + geom_histogram() + facet_grid(type ~ .)
> print(q)
> p <- ggplot(temp, aes(x = counts, fill = type)) + xlab("") +
+     ylab("")
> q <- p + geom_density() + facet_grid(type ~ .)
> print(q)

geoms-016.jpg



geom_step

> temp <- data.frame(walk = cumsum(runif(10)), x = 1:10)
> p <- ggplot(temp, aes(x = x))
> g <- p + geom_step(aes(y = walk))
> print(g)
> g <- p + geom_step(aes(y = walk), direction = "hv")
> print(g)
> g <- p + geom_step(aes(y = walk), direction = "vh")
> print(g)
> temp <- data.frame(walk = cumsum(runif(10)), x = 1:10, type = sample(c("a",
+     "b"), 10, T))
> p <- ggplot(temp, aes(x = x, colour = type))
> g <- p + geom_step(aes(y = walk))
> print(g)

geoms-017.jpg



geom_hline

> p <- ggplot(temp, aes(x = x, y = walk))
> q <- p + geom_point() + geom_hline(yintercept = 2)
> print(q)
> q <- p + geom_point() + geom_hline(yintercept = seq(-1, 3, 1))
> print(q)
> q <- p + geom_point() + geom_hline(yintercept = seq(-1, 3, 1)) +
+     facet_grid(type ~ .)
> print(q)

geoms-018.jpg



geom_vline

> p <- ggplot(temp, aes(x = x, y = walk))
> q <- p + geom_point() + geom_vline(xintercept = 2)
> print(q)
> q <- p + geom_point() + geom_vline(xintercept = seq(-1, 3, 1))
> print(q)
> q <- p + geom_point() + geom_vline(xintercept = seq(1, 10, 1)) +
+     facet_grid(type ~ .)
> print(q)

geoms-019.jpg



geom_point

> p <- ggplot(temp, aes(x = x, y = walk))
> q <- p + geom_point()
> print(q)
> p <- ggplot(temp, aes(x = x, y = walk))
> q <- p + geom_point(size = 4, colour = "red")
> print(q)
> q <- p + geom_point(aes(colour = type))
> print(q)
> q <- p + geom_point(aes(colour = type, shape = type))
> print(q)
> p <- ggplot(master, aes(x = ticker.id, y = market.cap, size = market.cap))
> q <- p + geom_point()
> print(q)
> p <- ggplot(master, aes(x = ticker, y = market.cap, size = market.cap))
> q <- p + geom_point() + coord_flip()
> print(q)
> p <- ggplot(master, aes(x = sector.name, y = market.cap/5000,
+     size = market.cap/5000))
> q <- p + geom_point() + coord_flip() + xlab("Sector") + ylab("Market Capitalization")
> print(q)

geoms-020.jpg



geom_jitter

> df <- data.frame(x = rnorm(2000), y = rnorm(2000))
> norm <- ggplot(df, aes(x, y))
> q <- norm + geom_point()
> print(q)
> q <- norm + geom_point(shape = 1)
> print(q)
> q <- norm + geom_point(shape = ".")
> print(q)
> q <- norm + geom_point(colour = alpha("black", 1/3))
> print(q)
> q <- norm + geom_point(colour = alpha("black", 1/5))
> print(q)
> q <- norm + geom_point(colour = alpha("black", 1/10))
> print(q)

geoms-021.jpg



geom_path

> temp <- as.data.frame(cbind(security.db1[, y.t], security.db1[,
+     x.t]))
> colnames(temp) <- c(y.t, x.t)
> temp$dates <- as.Date(dates)
> p <- ggplot(temp, aes(x = dates))
> q <- p + geom_path(aes(y = UNIONBANK))
> print(q)
> q <- p + geom_path(aes(y = UNIONBANK, color = dates))
> print(q)
> p <- ggplot(temp, aes(x = dates, y = UNIONBANK))
> q <- p + geom_path(colour = "sienna")
> print(q)

geoms-022.jpg



geom_rug

> p <- ggplot(temp, aes(x = UNIONBANK, y = PNB))
> g <- p + geom_point() + geom_rug()
> print(g)
> g <- p + geom_point() + geom_rug(position = "jitter")
> print(g)

geoms-023.jpg



geom_ribbon

> dim(temp)
[1] 256   3
> temp$y1 <- temp$UNIONBANK + 10 * runif(256)
> temp$y2 <- temp$UNIONBANK - 10 * runif(256)
> p <- ggplot(temp, aes(dates))
> q <- p + geom_ribbon(aes(ymin = y1, ymax = y2))
> print(q)

geoms-024.jpg

So, that was my saturday … Used ggplot to draw 52 graphs…Aimed for 100 and reached 50..

So, I guess the stuff that I believe is going to be correct.. If you aim for the stars , you will atleast hit the moon.

Let the world think whatever they want to think..I want to be resourceful, passionate about statistics . I want to create so many tools, programs, high frequency programs, trading algos that before I die, I w