How to plot a mosaicplot for a 1x1 contingency table?


Plotting a 1-by-1 contingency table returns an error:

dat <- read.table(textConnection(' foo bar TRUE TRUE TRUE TRUE '), header = TRUE, colClasses=c('logical', 'logical')) mosaicplot(table(dat)) Error in rep.int(0, ydim) : invalid 'times' value

As I learned, the code in the mosaicplot function doesn't allow to plot a 1-by-1-table. But then, how <em>do</em> I plot a mosaicplot of that table?

<hr />


I am plotting a series of dynamically created tables, some of which sometimes happen to have only one column and one row, at other times they have more dimensions. Having an undivided rectangle in that series of mosaicplots is valuable information and easily grasped in that visual representation.


One possibility is to coerce the variables to be plotted to factor and specify the possible outcomes in levels (in the desired order). Then zero count cells will be represented as thin lines.

dat[] = lapply(dat, factor, levels = c(TRUE, FALSE)) mosaicplot(table(dat))


If you don't want the thin lines of <em>@Henrik</em>'s (<em>great!</em>) solution you could substitute one-dimensional cases with a custom plot function.

mpOneDim <- function(tbl, title="table(dat)") { dn <- attr(tbl, "dimnames") labs <- names(dn) levels <- unlist(dn) plot.new() rect(0.0125, -.035, .985, .99, col="gray", border=1) mtext(title, line=1.55, font=2, cex=1.2) mtext(labs[1], 1, 1) mtext(labs[2], 2, 1) mtext(levels[2], 2, -1.15, cex=.7) mtext(levels[2], 3, -.75, cex=.7) } mpOneDim(table(dat))

In your function do something like this:

if (sum(dim(table(dat))) <= 2) { mpOneDim(table(dat)) } else { mosaicplot(table(dat)) } <hr />


dat <- structure(list(foo = c(TRUE, TRUE), bar = c(TRUE, TRUE)), class = "data.frame", row.names = c(NA, -2L))



  • How to plot a mosaicplot for a 1x1 contingency table?
  • How to do static analysis for C++ code with Eclipse cdt?
  • Qt debugging error: “while parsing target library list: not well-formed (invalid token)”
  • Control access to WebDav/Apache using Python
  • Instagram Authentification with python and requests
  • How to add CKEditor RTE to typo3 Backend Module with the API?
  • Retrieve 3rd MAX salary in Hive
  • Ruby on Rails - Settting up Reviews functionality
  • Tortoise SVN conflict occurs but no edit conflict button is shown
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Arc gradients in Flutter?
  • Java 11 and E(fx)clipse JavaFX plugin on Eclipse 4.9: An error has occurred - see the log file
  • Threads and Concurrent Modification Exception working with a list
  • can you use embedded ruby in custom javascript files in rails?
  • android : speech recognition what are the technologies available
  • $this->a->b->c->d calling methods from a superclass in php
  • Yii Bootstrap not loading JS files
  • How to display youtube video on HTML page? [closed]
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • How to select multiple items from a List view - JavaFX 8
  • SQL Server 2012 not showing unicode character in results
  • All Event listing on specified date in Google Calender api (V3) in java?
  • How to split wav file into two or more parts using c#
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • C++ STL stack pop operation giving segmentation fault
  • Calculate time from document
  • How to specify generic type when the type is only known at runtime?
  • Android: Unable to detect vertical plane
  • Why isn't stemDocument stemming?
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • Codeigniniter insert data through models and controller
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • Firebase: How to read from external DB?
  • WPF custom control and direct content support
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?