Elegant header graphics using ggplot2

The header image is fully drawn using ggplot2.

Here is a complete code to generate the image:


## option for supressing axis texts, axis titles, and legends.
opt <- opts(legend.position="none", axis.title.x=theme_blank(), axis.title.y=theme_blank(),
  axis.text.x=theme_blank(), axis.text.y=theme_blank())

## list for ggplot layers
p <- list()

## Mandelbrot set
# calculation
z <- 0
for (k in 1:33) z <- z^2+outer(-360:120,1i*-240:240,'+')/200
z <- data.frame(expand.grid(x=seq(ncol(z)), y=seq(nrow(z))), z=as.vector(exp(-abs(z))))
# make a ggplot layer
p[[1]] <- ggplot(z, aes(x, y, fill=z)) + geom_tile() + opt

## The randomness
# data
d <- data.frame(x=runif(32), y=runif(32), s=100*runif(32), f=factor(sample(8,32,TRUE)))
# Density estimation and making a layer
p[[2]] <- ggplot(d, aes(x, y, colour=f)) + geom_point(alpha=0.3, size=25) + opt

## Lorentz atractor using fNonlinear library - calculation and layer
p[[3]] <- ggplot(data.frame(lorentzSim(doplot=FALSE)), aes(x, z, colour=y, group=1)) +
  scale_colour_gradient2(low=(col <- brewer.pal(3, "Spectral"))[1], mid=col[2], high=col[3]) +
  geom_path() + opt + opts(panel.background=theme_rect(fill = "grey5", colour = NA))

## Dragon curve
# recursive function
f <- function(ax,ay,bx,by,n){
  xx <- bx-ax
  yy <- ay-by
  cx <- ax+(xx+yy)/2
  cy <- by+(xx+yy)/2
  if (n==0)
    return(data.frame(x=c(ax, bx), y=c(ay, by), xend=cx, yend=cy))
    return(rbind(Recall(ax, ay, cx, cy, n-1),
                 Recall(bx, by, cx, cy, n-1)))
# calculation and layer
p[[4]] <- ggplot(f(170, 140, 400, 350, 12), aes(x, y, xend=xend, yend=yend)) + geom_segment() + opt

## Draw ggplot grobs to png file
png(width=760, height=190)
pushViewport(viewport(layout=grid.layout(1, 4)))
for (pp in 1:4) print(p[[pp]], vp=viewport(layout.pos.col=pp))

One thought on “Elegant header graphics using ggplot2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s