2x2 mosaic Viewport error: cell was not found

163 views Asked by At

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?

1

There are 1 answers

0
Achim Zeileis On

You visualized the transposed table t(t0) with mosaic() but then try to add labeling_cells() for the original table t0. As the two tables don't match, the labeling cannot find the viewports it expects. Simply use t(t0) for the labeling as well:

mosaic(t(t0), split = TRUE, shade = TRUE, pop = FALSE)
labeling_cells(text = labs, margin = 0)(t(t0))

mosaic