library("vcd")Loading required package: grid
library("scatterplot3d")
library(colorspace)
## random seed
myseed <- 1071
set.seed(myseed)Loading required package: grid

###################################################
### Figure 12.2: 3D bar chart for the hospital data
###################################################
myHospital = t(Hospital)[,3:1]
mydat = data.frame("Length of stay" = as.vector(row(myHospital)),
"Visit frequency" = as.vector(col(myHospital)),
"Number of patients" = as.vector(myHospital))
scatterplot3d(mydat, type = "h", pch = " ", lwd = 10,
x.ticklabs = c("2-9","","10-19","","20+"),
y.ticklabs = c("Never","","Less than monthly","","Regular"),
xlab = "Length of stay", ylab = "Visit frequency", zlab = "Number of patients",
y.margin.add = 0.2,
color = "black", box = FALSE)



par(mfrow = c(1,1), mar = c(1,1,1,1), oma = c(0,0,0,0))
plot.new()
rect(0:4/5, 0.2, 1:5/5, 0.5, border = 0, col = diverge_hcl(5))
rect(0, 0.2, 1, 0.5, border = 1, col = NULL)
rect(0:4/5, 0.55, 1:5/5, 0.85, border = 0, col = diverge_hsv(5))
rect(0, 0.55, 1, 0.85, border = 1, col = NULL)
text(c(1:5/5 - 0.1), 0.11, c("(260, 100, 50)", "(260, 50, 70)", "(H, 0, 90)", "(0, 50, 70)", "(0, 100, 50)"))
text(c(1:5/5 - 0.1), 0.91, c("(240, 100, 100)", "(240, 50, 100)", "(H, 0, 100)", "(0, 50, 100)","(0, 100, 100)"))
mycol <- rep(grey.colors(2)[2:1], 1:2)
mosaic(t(Hospital),
mar = c(left = 3.5),
labeling_args = list(offset_labels = c(left = 0.5),
offset_varnames = c(left = 1, top = 0.5), set_labels =
list("Visit frequency" = c("Regular","Less than\nmonthly","Never"))),
split = TRUE, highlighting = 2, gp = gpar(fill = mycol, col = mycol))
### color coding of the residuals
mosaic(t(Hospital), split = TRUE, shade = TRUE,
mar = c(left = 3.5),
gp_args = list(p.value = hos_chisq$p.value),
labeling_args = list(offset_labels = c(left = 0.5),
offset_varnames = c(left = 1, top = 0.5), set_labels =
list("Visit frequency" = c("Regular","Less than\nmonthly","Never"))))
