File: plot.bounds.R

package info (click to toggle)
r-cran-eipack 0.2-2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 476 kB
  • sloc: ansic: 1,155; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
plot.bounds <- function(x, row, column, labels = TRUE, order =
                        NULL, intersection = TRUE, xlab, ylab, col = par("fg"),
                        lty = par("lty"), lwd = par("lwd"), ...){ 
  
  if(inherits(x,"bounds")==FALSE){
    stop("'x' must be output from 'bounds'")
  }

  bounds <- x$bounds
  idx <- paste(row, ".", column, sep="")
  if (!(idx %in% names(bounds))){
    stop("selected row or column bounds not in 'x' - please choose a different row or column")
  }

  if(all(is.na(bounds[[idx]]))){
    stop("selected row or column bounds not in 'x' - no precincts satisfy threshold")
  }

  "%wo%" <- function(x,y){x[!x %in% y]}
  threshold <- 100*x$threshold

  if(is.null(order)){
    order <- (1:nrow(bounds[[idx]]))/(nrow(bounds[[idx]])+1)
    xl <- 0:1
    axes <- FALSE
  }
  else {
    xl <- range(order)
    axes <- TRUE
  }
  if (missing(xlab)) {
    xlab <- paste("Precincts with at least", threshold, "% ", row)
  }
  if (missing(ylab)) {
    ylab <- paste("Proportion ", column, sep="")
  }
  plot(xl, 0:1, type = "n", xlab = xlab, ylab = ylab,
       axes = axes, ...)
  axis(side = 2, labels=TRUE)  

  segments(order, bounds[[idx]][,"lower"], order, bounds[[idx]][,"upper"], col = col,
           lty = lty, lwd = lwd)

  if(labels){
    text(order, bounds[[idx]][,"upper"]+.02,
         rownames(bounds[[idx]]), cex=0.4)
  }
  
  if(intersection){
    if(!all(is.na(x$intersection[[row]]))){
      abline(h=c(x$intersection[[row]][1],
               x$intersection[[row]][2]), col="grey80")
    }
  }
}