demo/election.R

### Pairs vs zen plot for the German election data

require(zenplots)

data(GerElection)
dat <- GerElection[,58:69] # Extract the data we work with
colnames(dat) <- c("SPD '05", "CDU/CSU '05", "Gruene '05", "FDP '05", "Linke '05", "Rest '05",
                   "SPD '02", "CDU/CSU '02", "Gruene '02", "FDP '02", "Linke '02", "Rest '02") # nicer names
x <- scale01(dat) #, method="pobs") # scaling to [0,1]

## Pairs plot
pairs(x, gap=0, pch=".") # pairs plot

## Zen plot
## We choose a path through all pairs of variables such that only pairs with
## equal year ('02 or '05) or with equal label (one of the parties or 'Rest') appear
ord <- c(1, 7, 8, 2, 3, 9, 10, 4, 5, 11, 12, 6) # path through the variables
zenplot(x[,ord], plot1d=function(x, plotID, ...)
    label_1d_graphics(x=x, labels=plotID$name, ...),
        plot2d=function(x, plotID, ...) {
            rect_2d_graphics(loc.xleft=-0.02, loc.ybottom=-0.02,
                             loc.xright=1.02, loc.ytop=1.02,
                             col="grey90", lty=2, ...)
            points_2d_graphics(x=x, add=TRUE, ...)
            label_2d_graphics(loc.x=0.04, loc.y=0.96, adj=c(0,1),
                              x=x, plotID=plotID, add=TRUE, cex=0.6, col="royalblue3",
                              labels=paste0("(",plotID$name[1],", ",plotID$name[2],")"), ...)
    })

Try the zenplots package in your browser

Any scripts or data that you put into this service are public.

zenplots documentation built on May 2, 2019, 4:34 p.m.