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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| library(knitr) classTable <- apply(Titanic,MARGIN = c(4,1), FUN = sum) kable(classTable)
classTotals <- apply(classTable, MARGIN = 2, FUN =sum) classSurvival <- t(classTable["Yes", ]/classTotals) rownames(classSurvival) <- c("survived") kable(classSurvival)
newTable <- 100 * round(classSurvival, 2) newTable <- t(newTable)
descendingOrder <- order(newTable, decreasing = TRUE) newTable <- newTable[descendingOrder, ,drop = FALSE] colnames(newTable) <- c("% survived") kable(newTable, caption = "survival rates on the titanic by class")
nvals <- nrow(newTable) col <- rainbow(nvals, alpha = 0.5) barplot(newTable, col = col, horiz = TRUE, axes = FALSE, names.arg = c(""), xlab = colnames(newTable)) xlocs <- cumsum(newTable) centres <- c(xlocs[1]/2, xlocs[1:(nvals - 1)] + diff(xlocs)/2) text(centres, 0.75, labels = rownames(newTable))
barplot(newTable, col = col, horiz = TRUE, beside = TRUE, names.arg = c(""), xlab = colnames(newTable), legend.text = rownames(newTable))
survivalProportions <- classTable survivalProportions["Yes",] <- survivalProportions["Yes", ]/classTotals survivalProportions["No",] <- survivalProportions["No", ]/classTotals survivalCols <- adjustcolor(c("black", "grey"), 0.5) barplot(survivalProportions, col = survivalCols, horiz = TRUE, beside = TRUE, xlab = "proportion of class", xlim = c(0,1)) legend("bottomright", title = "survival", fill = survivalCols, legend = rownames(survivalProportions))
barplot(survivalProportions, col = survivalCols, horiz = TRUE, beside = FALSE, xlab = "proportion of class", space = 0)
barplot(apply(classTable, MARGIN = 2, FUN =sum), col = adjustcolor("steelblue", 0.5), xlab = "class", ylab = "number of passengers") barplot(classTable, col=survivalCols, xlab = "class", ylab = "numebr of passengers")
library(loon.data) data("SAheart", package = "loon.data") kable(head(SAheart))
noFamilyHistory <- SAheart[,"famhist"] == "Absent" FamilyHistory <- SAheart[,"famhist"] == "Present" famHIsotryCol <- adjustcolor("steelblue", 0.5) noHistoryCol <- adjustcolor("firebrick", 0.5) boxplot(SAheart[noFamilyHistory, "sbp"], col = famHIsotryCol, main = "No family history", horizontal = TRUE)
boxplot(sbp~famhist, data = SAheart, col = c(noHistoryCol, famHIsotryCol), main = "systolic blood pressure", horizontal = TRUE)
library(ggplot2) ggplot(data=SAheart, mapping = aes(x=famhist, y = sbp)) + geom_boxplot(colour = c("firebrick", "steelblue"), fill = c("firebrick", "steelblue"), alpha = 0.5) + coord_flip()
hist(SAheart[noFamilyHistory, "sbp"], col = noHistoryCol, main = "No family history", xlab = "systolic blood pressure", xlim = extendrange(SAheart[,"sbp"]), ylim = c(0, 60))
hist(SAheart[noFamilyHistory, "sbp"], col = noHistoryCol, main = "No family history", xlim = extendrange(SAheart[,"sbp"])) hist(SAheart[FamilyHistory, "sbp"], col = famHIsotryCol, xlim = extendrange(SAheart[,"sbp"]), add = TRUE)
xrange <- extendrange(SAheart[,"sbp"]) breaks <- seq(xrange[1], xrange[2], length.out = 12) h1 <- hist(SAheart[noFamilyHistory,"sbp"], breaks = breaks, plot = FALSE) h2 <- hist(SAheart[FamilyHistory,"sbp"], breaks = breaks, plot = FALSE) hmax = max(c(h1$counts, h2$counts)) h2$counts <- -h2$counts hmin = -hmax X = c(h1$breaks, h2$breaks) xmax <- max(X) xmin = min(X) plot(h1, xlab = "systolic blood pressure", main = "comapring patients with(blue) and without(pink)", ylim = c(hmin, hmax),xlim = c(xmin, xmax), col = noHistoryCol) lines(h2, col = famHIsotryCol)
yrange <- extendrange(SAheart[,"sbp"]) breaks <- seq(yrange[1], yrange[2], length.out = 12) h1 <- hist(SAheart[noFamilyHistory,"sbp"], breaks = breaks, plot = FALSE) h2 <- hist(SAheart[FamilyHistory,"sbp"], breaks = breaks, plot = FALSE) nbreaks <- length(breaks) hmax = max(c(h1$counts, h2$counts)) h2$counts <- -h2$counts hmin = -hmax Y <- rep(h1$breaks, each = 2) X <- c(0,rep(h1$counts, each = 2), 0) plot(rep(0,2), range(Y), type = "l", col = "black", xlim = c(hmin, hmax), ylim = extendrange(Y), bty = "n", xlab = "frequency", ylab = "systolic blood pressure", main = "comparing patients with and without") polygon(X, Y, col = noHistoryCol) for (i in 1:nbreaks){lines(c(0, h1$counts[i]), c(rep(h1$breaks[i+1], 2)))} Y <- rep(h2$breaks, each = 2) X <- c(0,rep(h2$counts, each = 2), 0) polygon(X, Y, col = famHIsotryCol) for(i in 1:nbreaks){lines(c(0,h2$counts[i]), c(rep(h2$breaks[i+1],2)))}
library(ggplot2) ggplot(data =SAheart, mapping = aes(x =sbp)) + geom_histogram(bins =12, colour = "grey50", fill="white") + facet_grid(famhist~.)
savepAr <- par(mfrow=c(1,2)) densAbsent <- density(SAheart[noFamilyHistory,"sbp"], bw = "SJ") densPresent <- density(SAheart[FamilyHistory,"sbp"], bw = "SJ")
plot(densAbsent, col = "firebrick", main = "no family history") polygon(densAbsent, col = noHistoryCol) plot(densPresent, col = "steelblue", main = "family history") polygon(densPresent, col = famHIsotryCol) par(savepAr)
savep <- par(mfrow=c(2,1)) xlim <- extendrange(SAheart[,"sbp"]) ylim <- extendrange(c(densAbsent$y, densPresent$y)) plot(densAbsent, col = "firebrick", main = "no family history", xlim = xlim, ylim = ylim) polygon(densAbsent, col = noHistoryCol) plot(densPresent, col = "steelblue",main = "family history", xlim = xlim, ylim = ylim) polygon(densPresent, col = famHIsotryCol) par(savep)
plot(densAbsent, col = "firebrick", xlab = "systolic blood pressure", main = "comparing a no family history with family history", xlim = xlim, ylim = ylim) polygon(densAbsent, col = noHistoryCol) lines(densPresent, col = "steelblue") polygon(densPresent, col = famHIsotryCol)
densPresent$y <- - densPresent$y ylim2 <- extendrange(c(densAbsent$y, densPresent$y)) plot(densAbsent, col = "firebrick", xlab = "systolic blood pressure", main = "comparing a no family history with family history", xlim = xlim, ylim = ylim2) polygon(densAbsent, col = noHistoryCol) lines(densPresent, col = "steelblue") polygon(densPresent, col = famHIsotryCol)
ylim3 <- extendrange(SAheart[,"sbp"]) xlim2 <- extendrange(c(densAbsent$y, densPresent$y)) xyswitch <- function(xy_thing){ yx_thing <- xy_thing yx_thing$x <- xy_thing$y yx_thing$y <- xy_thing$x yx_thing } plot(xyswitch(densAbsent), col = "firebrick", xlab = "Density", ylab = "systolic blood pressure", main = "comparing a no family history with family history", xlim = xlim2, ylim = ylim3) polygon(xyswitch(densAbsent), col = noHistoryCol) lines(xyswitch(densPresent), col = "steelblue") polygon(xyswitch(densPresent), col = famHIsotryCol)
libarary(ggplot2) ggplot(data=SAheart, mapping = aes(x=sbp, col = famhist)) + geom_density(colour = "grey50", fill = "black", alpha = 0.4, bw = "SJ") + facet_grid(famhist~.)
xlim <- extendrange(SAheart[,"sbp"]) n <- nrow(SAheart) col = rep(adjustcolor("firebrick", 0.2),n) col[FamilyHistory] <- adjustcolor("steelblue", 0.2) Y <- rep(1,n) Y[FamilyHistory] <- -1 plot(SAheart[,"sbp"], y = Y, col = col, pch = 19, cex = 3, xlab = "systolic blood pressure", main = "comparing a no family history with family history", xlim = xlim, ylim = c(-2,2), bty = "n", yaxt = "n")
xlim <- extendrange(SAheart[,"sbp"]) n <- nrow(SAheart) col = rep(adjustcolor("firebrick", 0.2),n) col[FamilyHistory] <- adjustcolor("steelblue", 0.2) Y <- rep(1,n) Y[FamilyHistory] <- -1 U <- runif(n, -0.3,0.3) plot(SAheart[,"sbp"], y = Y + U, col = col, pch = 19, cex = 3, xlab = "systolic blood pressure", main = "comparing a no family history with family history", xlim = xlim, ylim = c(-2,2), bty = "n", yaxt = "n")
savePar <- par(mfrow = c(1,2)) nAbsent <- sum(noFamilyHistory) nPresent <- sum(FamilyHistory) pAbsent <- ppoints(nAbsent) pPresent <- ppoints(nPresent)
plot(pAbsent,sort(SAheart[noFamilyHistory, "sbp"]), type = "b", col = noHistoryCol, pch =19, xlab = "cumulative proportion", ylab = "systolic blood pressure", main = "no family histroy")
plot(pPresent,sort(SAheart[FamilyHistory, "sbp"]), type = "b", col = famHIsotryCol, pch =19, xlab = "cumulative proportion", ylab = "systolic blood pressure", main = "family histroy") par(savePar)
ylim <- extendrange(SAheart[,"sbp"]) nAbsent <- sum(noFamilyHistory) nPresent <- sum(FamilyHistory) pAbsent <- ppoints(nAbsent) pPresent <- ppoints(nPresent)
plot(pAbsent,sort(SAheart[noFamilyHistory, "sbp"]), type = "b", col = noHistoryCol, pch =19, xlab = "cumulative proportion", ylab = "systolic blood pressure", main = "no family histroy") points(pPresent, sort(SAheart[FamilyHistory, "sbp"]), type = "b", col = famHIsotryCol, pch =19)
|