|
8 | 8 |
|
9 | 9 | plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){ |
10 | 10 |
|
11 | | - s <- summaryPop(N=N, |
12 | | - confidence=confidence/100, |
13 | | - tails=ifelse(tails=='Interval',2,1), |
14 | | - abovethresh=popthresh) |
| 11 | + s <- summaryPop(N = N, |
| 12 | + confidence = confidence/100, |
| 13 | + tails = ifelse(tails=='Interval',2,1), |
| 14 | + abovethresh = popthresh, |
| 15 | + round_result = FALSE) |
15 | 16 |
|
16 | 17 | # plot margins |
17 | 18 | par(mar=c(4.5,1,4.5,0.5)) |
@@ -50,8 +51,19 @@ plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){ |
50 | 51 | s$lower <- 0 |
51 | 52 | } |
52 | 53 |
|
53 | | - ilow <- min(which(d$x >= s$lower)) |
54 | | - iup <- max(which(d$x <= s$upper)) |
| 54 | + # ilow <- min(which(d$x >= s$lower)) |
| 55 | + # iup <- max(which(d$x <= s$upper)) |
| 56 | + |
| 57 | + if(any(d$x >= s$lower)){ |
| 58 | + ilow <- min(which(d$x >= s$lower)) |
| 59 | + } else{ |
| 60 | + ilow <- 1 |
| 61 | + } |
| 62 | + if(any(d$x <= s$upper)){ |
| 63 | + iup <- max(which(d$x <= s$upper)) |
| 64 | + } else{ |
| 65 | + iup <- nrow(d) |
| 66 | + } |
55 | 67 |
|
56 | 68 | dsub <- d[ilow:iup,] |
57 | 69 | dsub <- rbind(data.frame(x=rep(s$lower,2), |
@@ -94,34 +106,40 @@ plotPop <- function(N, confidence=95, tails='Interval', popthresh=100){ |
94 | 106 |
|
95 | 107 | if(is.numeric(N)){ |
96 | 108 |
|
| 109 | + # data |
| 110 | + pop_mean <- ifelse(s$mean > 5, round(s$mean), round(s$mean,1)) |
| 111 | + pop_lower <- ifelse(s$mean > 5, round(s$lower), round(s$lower,1)) |
| 112 | + pop_upper <- ifelse(s$mean > 5, round(s$upper), round(s$upper,1)) |
| 113 | + pop_abovethresh <- round(s$abovethresh*100) |
| 114 | + |
97 | 115 | # Main title |
98 | 116 | line <- 3 |
99 | | - mtext(paste0('Population Estimate: ',prettyNum(s$mean,big.mark=','),' people'), |
| 117 | + mtext(paste0('Population Estimate: ',prettyNum(pop_mean,big.mark=','),' people'), |
100 | 118 | line=line, cex=1.5) |
101 | 119 |
|
102 | 120 | # Sub title A |
103 | 121 | line <- 1.5 |
104 | 122 | cex <- 1.25 |
105 | 123 | if(tails=='Interval'){ |
106 | 124 |
|
107 | | - mtext(paste0(round(confidence),'% probability: ', prettyNum(s$lower,big.mark=','),' - ', prettyNum(s$upper, big.mark=','),' people'), |
| 125 | + mtext(paste0(round(confidence),'% probability: ', prettyNum(pop_lower,big.mark=','),' - ', prettyNum(pop_upper, big.mark=','),' people'), |
108 | 126 | line=line, cex=cex) |
109 | 127 |
|
110 | 128 | } else if(tails=='Lower Limit'){ |
111 | 129 |
|
112 | | - mtext(paste0(round(confidence),'% probability: > ', prettyNum(s$lower,big.mark=','), ' people'), |
| 130 | + mtext(paste0(round(confidence),'% probability: > ', prettyNum(pop_lower,big.mark=','), ' people'), |
113 | 131 | line=line, cex=cex) |
114 | 132 |
|
115 | 133 | } else if(tails=='Upper Limit'){ |
116 | 134 |
|
117 | | - mtext(paste0(round(confidence),'% probability: < ', prettyNum(s$upper,big.mark=','),' people'), |
| 135 | + mtext(paste0(round(confidence),'% probability: < ', prettyNum(pop_upper,big.mark=','),' people'), |
118 | 136 | line=line, cex=cex) |
119 | 137 | } |
120 | 138 |
|
121 | 139 | # Sub title B |
122 | 140 | if(is.numeric(popthresh)){ |
123 | 141 | line <- 0.25 |
124 | | - mtext(paste0(round(s$abovethresh*100),'% probability: > ', prettyNum(popthresh,big.mark=','), ' people (threshold)'), |
| 142 | + mtext(paste0(pop_abovethresh,'% probability: > ', prettyNum(popthresh,big.mark=','), ' people (threshold)'), |
125 | 143 | line=line, cex=cex) |
126 | 144 | } |
127 | 145 | } |
|
0 commit comments