<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/"></div></div><div><a data-url="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/" data-counturl="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/" data-count="horizontal" data-text="Troubles with cell labels in mosaic plots… and how to solve them." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rcrastinate.blogspot.com/2016/07/troubles-with-cell-labels-in-mosaic.html"> Rcrastinate</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today I want to write about a solution to a quite specific problem. Suppose, you want to label cells in your ‘vcd’ package mosaic plots in a custom way. For example, we might want to use cell labels which indicate “too much” or “too few” cases (given your expected values). Such labels might be “+” and “-” (and maybe “++” and “–” for higher residuals).</p>
<p>We create our custom table:<br><span>tab &lt;- rbind(c(450, 230),</span><br><span>             c(200, 600))</span><br>We create our mosaic plot:<br><span>library(vcd)</span><br><span>mosaic(tab, shade = T)</span>
<div><a target="_blank" href="http://i1.wp.com/3.bp.blogspot.com/-K1pzWlrIPQA/V5sSClGJzxI/AAAAAAAACu8/lzLC5K1nP0kqW0Fpy_lBYJ_zXdGj3tj8QCLcB/s1600/mp1.png?ssl=1"><img src="http://i0.wp.com/3.bp.blogspot.com/-K1pzWlrIPQA/V5sSClGJzxI/AAAAAAAACu8/lzLC5K1nP0kqW0Fpy_lBYJ_zXdGj3tj8QCLcB/s400/mp1.png?resize=400%2C363&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div>So far, so good.</div>
<div></div>
<div>Now, we want to use custom labels for the cells.</div>
<div></div>
<div>Let’s first create a second table containing our labels:</div>
<div><span>lab.tab &lt;- rbind(c(“++”, “–“),</span></div>
<div><span>                 c(“–“, “++”))</span></div>
<div>Now, we use lab.tab in our call to mosaic():</div>
<div><span>mosaic(tab, labeling = labeling_cells(text = lab.tab))</span></div>
<div></div>
<div>We get this:</div>
<div><a target="_blank" href="http://i2.wp.com/4.bp.blogspot.com/-0sSfoKiQYA0/V5sSyDIqsOI/AAAAAAAACvE/iWpoT_Snal4lyRZhsnR_dj_SFui_5GKyACLcB/s1600/mp2.png?ssl=1"><img src="http://i1.wp.com/4.bp.blogspot.com/-0sSfoKiQYA0/V5sSyDIqsOI/AAAAAAAACvE/iWpoT_Snal4lyRZhsnR_dj_SFui_5GKyACLcB/s400/mp2.png?resize=400%2C363&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div>This is not what we wanted – only the first label in lab.tab is used. Bummer! Also, R throws some warnings at us (sorry, they’re in German):</div>
<div><span>Warnmeldungen:</span></div>
<div><span>1: In if (!is.na(txt)) txt :</span></div>
<div><span>  Bedingung hat Länge > 1 und nur das erste Element wird benutzt</span></div>
<div><span>2: In if (!is.na(txt)) txt :</span></div>
<div><span>  Bedingung hat Länge > 1 und nur das erste Element wird benutzt</span></div>
<div><span>3: In if (!is.na(txt)) txt :</span></div>
<div><span>  Bedingung hat Länge > 1 und nur das erste Element wird benutzt</span></div>
<div><span>4: In if (!is.na(txt)) txt :</span></div>
<div><span>  Bedingung hat Länge > 1 und nur das erste Element wird benutzt</span></div>
<div></div>
<div>The solution is to give correct ‘dimnames’ to our custom tables:</div>
<div><span>dimnames(tab) &lt;- list(VarA = c(“levelA”, “levelB”),</span></div>
<div><span>                      VarB = c(“levelA”, “levelB”))</span></div>
<div></div>
<div>Note, that we have to use named elements here for the dimnames-list (these names are “VarA” and “VarB” in the example)!</div>
<div></div>
<div>Now the warnings are gone and it works:</div>
<div><span>mosaic(tab, labeling = labeling_cells(text = lab.tab))</span></div>
<div><a target="_blank" href="http://i1.wp.com/2.bp.blogspot.com/-DUUjS3Qgtfs/V5sVDgNuWsI/AAAAAAAACvU/Q3FrNt24sWsVWbC7fRwoJ-NCsypNNnJAACK4B/s1600/mp3.png"><img src="http://i0.wp.com/2.bp.blogspot.com/-DUUjS3Qgtfs/V5sVDgNuWsI/AAAAAAAACvU/Q3FrNt24sWsVWbC7fRwoJ-NCsypNNnJAACK4B/s400/mp3.png?resize=400%2C363&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div>It took me some time to figure this out, because all the examples in ?mosaic, ?strucplot and ?labeling_cells already have a correct ‘dimnames’-structure. Maybe, this helps someone…</div>
<div></div>
<div></div>
<div></div>
<p></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/"></div></div><div><a data-url="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/" data-counturl="http://www.r-bloggers.com/troubles-with-cell-labels-in-mosaic-plots-and-how-to-solve-them/" data-count="vertical" data-text="Troubles with cell labels in mosaic plots… and how to solve them." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rcrastinate.blogspot.com/2016/07/troubles-with-cell-labels-in-mosaic.html"> Rcrastinate</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CWzq2X47Fh0"></a></div>