4

I have created a mosaic plot using mosaic function in the vcd package. Now I wish to add some annotations using labeling_cells. Unfortunately, I get an error. The problem might be that it is not the standard Titanic example...

library("grid"); library("vcd")
dataset <- read.table("http://bit.ly/1aJTI1C")

# prepare data for plot as a "Structured Contingency Table"
data1 <- structable(dauer ~ groesse + ort, dataset) 

# basic plot
mosaic(data1,
       # separate the two elements of the plot  
       split_vertical = c(T, T, F),

       # put the names in the right places and adds boxes 
       labeling_args = list(tl_labels = TRUE,
                            tl_varnames = FALSE,
                            boxes = TRUE),
       # grip remains open 
       pop=FALSE 
       )

enter image description here

# structure that matches plot, but it does not help
#match<-t(data1)

# try to add labels 
labeling_cells(text = data1, clip = FALSE)(data1)

This results in:

# Error in ifelse(abbreviate_varnames, sapply(seq_along(dn), function(i) abbreviate(dn[i],  : 
#   replacement has length zero
# In addition: Warning message:
# In rep(no, length.out = length(ans)) :
#  'x' is NULL so the result will be NULL

Another problem I have is that the boxes do not fit the labels. If you have a hint for that just let me know as well!

It's my first question here, so please excuse potential errors!

Thanks a lot!

Henrik
  • 65,555
  • 14
  • 143
  • 159
Karl A
  • 165
  • 11

2 Answers2

4

Fixed upstream in vcd 1.4-4, but note that you can simply use

mosaic(data1, labeling = labeling_values)
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
2

Yes, this is quite confusing and ought to be fixed in labeling_cells(). For some reason the data in the labeling should be a regular table, not a structable. I'll raise this with David, the principal author of mosaic() and package maintainer.

If you know it it's easy to work around it, though:

labeling_cells(text = as.table(data1), clip = FALSE)(as.table(data1))

mosaic labeling

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49