[英]labeling_cells function with 2d mosaic
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. 似乎有些事情(很多事情?)我不了解如何在软件包
vcd
和labeling_cells
使用mosaic
,因为我不断遇到错误。
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). mosaic(tmparray)
我提供了预期的绘图(rep太低而无法发布图像)。
I would like to add text labels for each cell showing only the values in tmparray > 1000
. 我想为每个单元格添加文本标签,仅显示
tmparray > 1000
的值。 Following the documentation for labeling_cells
, I try the following: 按照
labeling_cells
的文档,我尝试以下操作:
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"
. 好的,所以我假设使用
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. 这似乎可以很好地进行绘制,但是如果您要使用
labeling_cells
,则需要将其转换为表格,并确保添加命名维度。 You can do that with 你可以这样做
tmparray <- as.table(tmparray)
names(dimnames(tmparray))<-c("P","C")
So when you create celltext
now with 所以,当你创建
celltext
现在
celltxt <- ifelse(tmparray < 1000, NA, tmparray/1000)
It will also be a table with the same dimnames
. 它也将是一个具有相同
dimnames
的表。
The other important thing you must do is pass pop=F
to mosaic
. 您必须做的另一件事是将
pop=F
传递给mosaic
。 This will leave the grid viewport structure in place so that the labeling_cells
function can find the correct locations for the labels. 这样会将网格视口结构保留在适当的位置,以便
labeling_cells
函数可以找到labeling_cells
的正确位置。 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) (我确实将这些值四舍五入到小数点后一位,以使内容更整洁)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.