# Data manipulation # Find the winners getwinners <- function(const.int) { const.int$maxvotes <- do.call("pmax", const.int[,paste("Votes",c(parties.int,'Other'),sep='.')]) const.int$Winner <- "Other" for (p in parties.int) const.int$Winner <- ifelse(const.int[,paste('Votes',p,sep='.')]==const.int$maxvotes, p, const.int$Winner) const.int } # Graphics routines tcoord <- function(x,y,z) { co <- cos(30/360*2*pi) si <- sin(30/360*2*pi) tc <- list(co*log(x)-co*log(y),-si*log(x)-si*log(y)+log(z)) lapply(tc, function(z) ifelse(is.nan(z),10000,z)) } bdy <- function(x=0,y=0,z=0) { e <- max(x,y,z) vb <- seq(e,1-2*e,by=.01) va <- 0*vb+e vc <- 1-va-vb v <- c(va,vb,vc) if (y>0) v <- c(vb,va,vc) if (z>0) v <- c(vb,vc,va) n <- length(v)/3 tc <- tcoord(v[1:n], v[1:n+n], v[1:n+2*n]) } bdy.top <- function(eps=.05) tcoord(eps,eps,1-2*eps) bdy.botleft <- function(eps=.05) tcoord(eps,1-2*eps,eps) bdy.botright <- function(eps=.05) tcoord(1-2*eps,eps,eps) bdy.bot <- function(eps=.05) tcoord((1-eps)/2,(1-eps)/2,eps) bdy.topleft <- function(eps=.05) tcoord(eps,(1-eps)/2,(1-eps)/2) bdy.topright <- function(eps=.05) tcoord((1-eps)/2,eps,(1-eps)/2) triangleAxes <- function(eps=.05) { panel.xyplot(bdy(x=eps)[[1]],bdy(x=eps)[[2]], type='l', col='grey') panel.xyplot(bdy(y=eps)[[1]],bdy(y=eps)[[2]], type='l', col='grey') panel.xyplot(bdy(z=eps)[[1]],bdy(z=eps)[[2]], type='l', col='grey') bd <- tcoord(eps,(1-eps)/2,(1-eps)/2) llines(c(0,bd[[1]]), c(0,bd[[2]]), col='grey') bd <- tcoord((1-eps)/2,eps,(1-eps)/2) llines(c(0,bd[[1]]), c(0,bd[[2]]), col='grey') bd <- tcoord((1-eps)/2,(1-eps)/2,eps) llines(c(0,bd[[1]]), c(0,bd[[2]]), col='grey') }