I am trying to plot percentages in a simple 2x2 contingency table with vcd::mosaic
but I keep getting a Viewport
error. Here is how to reproduce (I work on Ubunto 20.04 and R 3.6.3):
t0 <- as.table(rbind( c(221,47), c(17,9) ))
names(dimnames(t0)) = c("X", "C")
rownames(t0) = c("neg", "pos")
colnames(t0) = c("neg", "pos")
library(vcd)
labs <- round(prop.table(t0, 1), 2)
mosaic(t(t0), split = TRUE, shade = TRUE, pop = FALSE )
labeling_cells(text = labs, margin = 0)(t0)
and I get with the last command:
labeling_cells(text = labs, margin = 0)(t0)
Error in grid.Call.graphics(C_downviewport, name$name, strict) : Viewport 'cell:X=neg,C=neg' was not found
Does anybody know why?
You visualized the transposed table
t(t0)
withmosaic()
but then try to addlabeling_cells()
for the original tablet0
. As the two tables don't match, the labeling cannot find the viewports it expects. Simply uset(t0)
for the labeling as well: