title: How to extract the calculated expected frequencies in a higher-dimensional mosaicplot in R
body: <p>After producing a table with three-variables as in:</p>
  
  <pre><code>salary &lt;- array(
    c(38, 12, 102, 141, 12, 9, 136, 383),
    dim=c(2, 2, 2),
    dimnames=list(exposure = c("exposed", "not"),
                  disease = c("case", "control"),
                  salary = c("&lt;1000", "&gt;=1000"))
  ) 
  
  salary
  , , salary = &lt;1000
  
           disease
  exposure  case control
    exposed   38     102
    not       12     141
  
  , , salary = &gt;=1000
  
           disease
  exposure  case control
    exposed   12     136
    not        9     383
  </code></pre>
  
  <p>the <code>vcd</code> package allows the creation of a mosaic plot like this:</p>
  
  <pre><code>mosaic(salary, shade = T)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7W0BJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7W0BJ.png" alt="enter image description here"></a></p>
  
  <p>in which the surface area is proportional to the counts in the cell, and the color corresponds to the standardized Pearson's residuals.</p>
  
  <p>The question is <strong>how to calculate and extract the expected counts?</strong></p>
  
  <p>I know that I can produce the margins like this:</p>
  
  <pre><code>addmargins(salary)
  , , salary = &lt;1000
  
           disease
  exposure  case control Sum
    exposed   38     102 140
    not       12     141 153
    Sum       50     243 293
  
  , , salary = &gt;=1000
  
           disease
  exposure  case control Sum
    exposed   12     136 148
    not        9     383 392
    Sum       21     519 540
  
  , , salary = Sum
  
           disease
  exposure  case control Sum
    exposed   50     238 288
    not       21     524 545
    Sum       71     762 833
  </code></pre>
  
  <p>and if it were a 2 x 2 contingency table I would use <code>chisq.test(x)$expected</code> to see the expected values, but I don't know how to go about it in a 3-way contingency table.</p>