There seems to be something (many things?) I don't understand about how to use mosaic
in package vcd
and labeling_cells
because I keep getting an error.
Setup:
I have a 2d array of values (row and column names changed to protect the innocent):
tmparray
C1 C2 C3 C4 C5 C6
P1 65241 15534 5754 12747 57902 48367
P2 205 28581 4819 7617 5504 34656
P3 27 3503 1473 757 3308 15363
P4 417580 5967 5353 636 514 9758
P5 31482 915 249 914 328 1301
P6 75501 4307 294 176 27 8376
P7 43589 2864 465 179 61 12631
P8 64095 1008 335 288 3 7438
mosaic(tmparray)
gets me the expected plot (rep too low to post image).
I would like to add text labels for each cell showing only the values in tmparray > 1000
. Following the documentation for labeling_cells
, I try the following:
celltxt <- ifelse(tmparray < 1000, NA, tmparray/1000)
celltxt
C1 C2 C3 C4 C5 C6
P1 65.241 15.534 5.754 12.747 57.902 48.367
P2 NA 28.581 4.819 7.617 5.504 34.656
P3 NA 3.503 1.473 NA 3.308 15.363
P4 417.580 5.967 5.353 NA NA 9.758
P5 31.482 NA NA NA NA 1.301
P6 75.501 4.307 NA NA NA 8.376
P7 43.589 2.864 NA NA NA 12.631
P8 64.095 1.008 NA NA NA 7.438
labeling_cells(text = celltxt)(tmparray)
Which results in the following error:
# Error in ifelse(abbreviate_varnames, sapply(seq_along(dn), function(i) abbreviate(dn[i], : replacement has length zero
Can anyone please point out how to get the labeling to work?
OK, so I'm assuming class(tmparray)=="matrix"
. That seems to be fine for plotting, but it looks like if you want to use labeling_cells
you're going to want to convert that to a table, being sure to add named dimensions. You can do that with
tmparray <- as.table(tmparray)
names(dimnames(tmparray))<-c("P","C")
So when you create celltext
now with
celltxt <- ifelse(tmparray < 1000, NA, tmparray/1000)
It will also be a table with the same dimnames
.
The other important thing you must do is pass pop=F
to mosaic
. This will leave the grid viewport structure in place so that the labeling_cells
function can find the correct locations for the labels. So you can create the plot with
mosaic(tmparray , pop=F)
labeling_cells(text = celltxt, margin=0)(tmparray)
Which results in
(I did round the values to one decimal place to try to make things a bit neater)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.