EVOLUTION-MANAGER
Edit File: labellers.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Useful labeller functions</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for labellers {ggplot2}"><tr><td>labellers {ggplot2}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Useful labeller functions</h2> <h3>Description</h3> <p>Labeller functions are in charge of formatting the strip labels of facet grids and wraps. Most of them accept a <code>multi_line</code> argument to control whether multiple factors (defined in formulae such as <code>~first + second</code>) should be displayed on a single line separated with commas, or each on their own line. </p> <h3>Usage</h3> <pre> label_value(labels, multi_line = TRUE) label_both(labels, multi_line = TRUE, sep = ": ") label_context(labels, multi_line = TRUE, sep = ": ") label_parsed(labels, multi_line = TRUE) label_wrap_gen(width = 25, multi_line = TRUE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>labels</code></td> <td> <p>Data frame of labels. Usually contains only one element, but faceting over multiple factors entails multiple label variables.</p> </td></tr> <tr valign="top"><td><code>multi_line</code></td> <td> <p>Whether to display the labels of multiple factors on separate lines.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>String separating variables and values.</p> </td></tr> <tr valign="top"><td><code>width</code></td> <td> <p>Maximum number of characters before wrapping the strip.</p> </td></tr> </table> <h3>Details</h3> <p><code>label_value()</code> only displays the value of a factor while <code>label_both()</code> displays both the variable name and the factor value. <code>label_context()</code> is context-dependent and uses <code>label_value()</code> for single factor faceting and <code>label_both()</code> when multiple factors are involved. <code>label_wrap_gen()</code> uses <code><a href="../../base/html/strwrap.html">base::strwrap()</a></code> for line wrapping. </p> <p><code>label_parsed()</code> interprets the labels as plotmath expressions. <code><a href="label_bquote.html">label_bquote()</a></code> offers a more flexible way of constructing plotmath expressions. See examples and <code><a href="../../base/html/bquote.html">bquote()</a></code> for details on the syntax of the argument. </p> <h3>Writing New Labeller Functions</h3> <p>Note that an easy way to write a labeller function is to transform a function operating on character vectors with <code><a href="as_labeller.html">as_labeller()</a></code>. </p> <p>A labeller function accepts a data frame of labels (character vectors) containing one column for each factor. Multiple factors occur with formula of the type <code>~first + second</code>. </p> <p>The return value must be a rectangular list where each 'row' characterises a single facet. The list elements can be either character vectors or lists of plotmath expressions. When multiple elements are returned, they get displayed on their own new lines (i.e., each facet gets a multi-line strip of labels). </p> <p>To illustrate, let's say your labeller returns a list of two character vectors of length 3. This is a rectangular list because all elements have the same length. The first facet will get the first elements of each vector and display each of them on their own line. Then the second facet gets the second elements of each vector, and so on. </p> <p>If it's useful to your labeller, you can retrieve the <code>type</code> attribute of the incoming data frame of labels. The value of this attribute reflects the kind of strips your labeller is dealing with: <code>"cols"</code> for columns and <code>"rows"</code> for rows. Note that <code><a href="facet_wrap.html">facet_wrap()</a></code> has columns by default and rows when the strips are switched with the <code>switch</code> option. The <code>facet</code> attribute also provides metadata on the labels. It takes the values <code>"grid"</code> or <code>"wrap"</code>. </p> <p>For compatibility with <code><a href="labeller.html">labeller()</a></code>, each labeller function must have the <code>labeller</code> S3 class. </p> <h3>See Also</h3> <p><code><a href="labeller.html">labeller()</a></code>, <code><a href="as_labeller.html">as_labeller()</a></code>, <code><a href="label_bquote.html">label_bquote()</a></code> </p> <h3>Examples</h3> <pre> mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma")) p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() # The default is label_value p + facet_grid(. ~ cyl, labeller = label_value) # Displaying both the values and the variables p + facet_grid(. ~ cyl, labeller = label_both) # Displaying only the values or both the values and variables # depending on whether multiple factors are facetted over p + facet_grid(am ~ vs+cyl, labeller = label_context) # Interpreting the labels as plotmath expressions p + facet_grid(. ~ cyl2) p + facet_grid(. ~ cyl2, labeller = label_parsed) </pre> <hr /><div style="text-align: center;">[Package <em>ggplot2</em> version 3.3.2 <a href="00Index.html">Index</a>]</div> </body></html>