Barplot
Nature1 <- matrix(c(33, 11, 6, 6, 12, 16), nrow = 2)
rownames(Nature1) <- c("Identical", "Fraternal")
colnames(Nature1) <- c("Alike", "Somewhat_Alike", "Not_Alike")
Nature1
## Alike Somewhat_Alike Not_Alike
## Identical 33 6 12
## Fraternal 11 6 16
options(digits = 3)
library(RColorBrewer)
cols <- brewer.pal(8, "Accent")
Nature1_p <- prop.table(Nature1, margin = 1) * 100
c1 <- ncol(Nature1)
b1 <- barplot(t(Nature1_p), space = 1.5, col = cols[1:3], yaxt = "n")
axis(side = 2,
at = apply(t(Nature1_p), MARGIN = 2, cumsum),
labels = format(apply(t(Nature1_p), MARGIN = 2, cumsum), digits = 3, nsmall = 1), las = 2)
y_text <- c(t(Nature1_p)[1, ] / 2,
t(Nature1_p)[1, ] + t(Nature1_p)[2, ] / 2,
t(Nature1_p)[1, ] + t(Nature1_p)[2, ] + t(Nature1_p)[3, ] / 2)
text(rep(b1, 3), y_text, labels = c(Nature1))
legend("top", fill = cols[3:1], legend = rev(colnames(Nature1)))
title(main = "Smoking Habits of Twins")

Mosaicplot
mosaicplot(Nature1,
col = cols[1:3],
main = "Smoking Habits of Twins",
xlab = "Twins",
ylab = "Resemblance",
cex.axis = 1)

Nature2 <- matrix(c(44, 9, 9, 9), nrow = 2)
rownames(Nature2) <- c("Identical", "Fraternal")
colnames(Nature2) <- c("Alike", "Not_Alike")
Nature2
## Alike Not_Alike
## Identical 44 9
## Fraternal 9 9
Nature2_p <- prop.table(Nature2, margin = 1) * 100
c2 <- ncol(Nature2)
b2 <- barplot(t(Nature2_p), space = 1.2, col = cols[1:2], yaxt = "n")
axis(side = 2,
at = apply(t(Nature2_p), MARGIN = 2, cumsum),
labels = format(apply(t(Nature2_p), MARGIN = 2, cumsum), digits = 3, nsmall = 1), las = 2)
y_text2 <- c(t(Nature2_p)[1, ] / 2,
t(Nature2_p)[1, ] + t(Nature2_p)[2, ] / 2)
text(rep(b2, 2), y_text2, labels = c(Nature2))
legend("top", fill = cols[2:1], legend = rev(colnames(Nature2)))
title(main = "Smoking Habits of Twins 2")

Living Separated or not
Barplot
Nature3 <- matrix(c(23, 21, 4, 5), nrow = 2)
rownames(Nature3) <- c("Lived Together", "Seperated")
colnames(Nature3) <- c("Alike", "Not_Alike")
Nature3
## Alike Not_Alike
## Lived Together 23 4
## Seperated 21 5
Nature3_p <- prop.table(Nature3, margin = 1) * 100
c3 <- ncol(Nature3)
b3 <- barplot(t(Nature3_p), space = 1.2, col = cols[1:2], yaxt = "n")
axis(side = 2,
at = apply(t(Nature3_p), MARGIN = 2, cumsum),
labels = format(apply(t(Nature3_p), MARGIN = 2, cumsum), digits = 3, nsmall = 1), las = 2)
y_text3 <- c(t(Nature3_p)[1, ] / 2,
t(Nature3_p)[1, ] + t(Nature3_p)[2, ] / 2)
text(rep(b3, 2), y_text3, labels = c(Nature3))
legend("top", fill = cols[2:1], legend = rev(colnames(Nature2)))
title(main = "Smoking Habits of Identical Twins")

Mosaicplot
mosaicplot(Nature3,
col = cols[3:2],
main = "Smoking Habits of Identical Twins",
xlab = "Lived Together?",
ylab = "Resemblance",
cex.axis = 1)

Retrospective Study by Doll & Hill
- By Number of Cigarettes Smoked
Barplot
DollnHill <- matrix(c(24, 38, 208, 242, 196, 201, 164, 118, 45, 23), nrow = 2)
rownames(DollnHill) <- c("Lung Cancer", "Control")
colnames(DollnHill) <- c("1-4", "5-14", "15-24", "25-49", "50 more")
DollnHill_p <- prop.table(DollnHill, margin = 1) * 100
DollnHill_p
## 1-4 5-14 15-24 25-49 50 more
## Lung Cancer 3.77 32.7 30.8 25.7 7.06
## Control 6.11 38.9 32.3 19.0 3.70
c4 <- ncol(DollnHill)
b4 <- barplot(t(DollnHill_p), space = 1.2, col = cols[1:5], yaxt = "n")
axis(side = 2,
at = apply(t(DollnHill_p), MARGIN = 2, cumsum),
labels = format(apply(t(DollnHill_p), MARGIN = 2, cumsum), digits = 2, nsmall = 1), las = 2)
y_text4 <- c(t(DollnHill_p)[1, ] / 2,
t(DollnHill_p)[1, ] + t(DollnHill_p)[2, ] / 2,
t(DollnHill_p)[1, ] + t(DollnHill_p)[2, ] + t(DollnHill_p)[3, ] / 2,
t(DollnHill_p)[1, ] + t(DollnHill_p)[2, ] + t(DollnHill_p)[3, ] + t(DollnHill_p)[4, ] / 2,
t(DollnHill_p)[1, ] + t(DollnHill_p)[2, ] + t(DollnHill_p)[3, ] + t(DollnHill_p)[4, ] + t(DollnHill_p)[5, ] / 2)
text(rep(b4, 5), y_text4, labels = c(DollnHill))
legend("top", fill = cols[5:1], legend = rev(colnames(DollnHill)))
title(main = "Retrospective Study : Doll & Hill")

Mosaicplot
mosaicplot(DollnHill,
col = cols[1:5],
main = "Retrospective Study : Doll & Hill",
xlab = "Group",
ylab = "Number of Cigarettes Smoked",
cex.axis = 1)

chisq.test(DollnHill)
##
## Pearson's Chi-squared test
##
## data: DollnHill
## X-squared = 20, df = 4, p-value = 4e-04
Barplot
DollnHill2 <- matrix(c(7, 17, 141, 162, 133, 157, 96, 74, 21, 16), nrow = 2)
rownames(DollnHill2) <- c("Lung Cancer", "Control")
colnames(DollnHill2) <- c("1-4", "5-14", "15-24", "25-49", "50 more")
DollnHill2_p <- DollnHill2 / DollnHill * 100
DollnHill2_p
## 1-4 5-14 15-24 25-49 50 more
## Lung Cancer 29.2 67.8 67.9 58.5 46.7
## Control 44.7 66.9 78.1 62.7 69.6
c5 <- ncol(DollnHill2)
b5 <- barplot(t(DollnHill2_p), space = 1.2, col = cols[1:5], yaxt = "n")
y_text5 <- c(t(DollnHill2_p)[1, ] / 2,
t(DollnHill2_p)[1, ] + t(DollnHill2_p)[2, ] / 2,
t(DollnHill2_p)[1, ] + t(DollnHill2_p)[2, ] + t(DollnHill2_p)[3, ] / 2,
t(DollnHill2_p)[1, ] + t(DollnHill2_p)[2, ] + t(DollnHill2_p)[3, ] + t(DollnHill2_p)[4, ] / 2,
t(DollnHill2_p)[1, ] + t(DollnHill2_p)[2, ] + t(DollnHill2_p)[3, ] + t(DollnHill2_p)[4, ] + t(DollnHill2_p)[5, ] / 2)
text(rep(b5, 5), y_text5, labels = paste0(format(c(DollnHill2_p), digits = 3, nsmall = 1), "%"))
legend("top", fill = cols[5:1], legend = rev(colnames(DollnHill2)))
title(main = "Percentage of Inhalers")

# mosaicplot(DollnHill2_p,
# col = cols[1:5],
# main = "Percentage of Inhalers",
# xlab = "Group",
# ylab = "Percentage")