EVOLUTION-MANAGER
Edit File: reorder_within.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: Reorder an x or y axis within facets</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 reorder_within {tidytext}"><tr><td>reorder_within {tidytext}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Reorder an x or y axis within facets</h2> <h3>Description</h3> <p>Reorder a column before plotting with faceting, such that the values are ordered within each facet. This requires two functions: <code>reorder_within</code> applied to the column, then either <code>scale_x_reordered</code> or <code>scale_y_reordered</code> added to the plot. This is implemented as a bit of a hack: it appends ___ and then the facet at the end of each string. </p> <h3>Usage</h3> <pre> reorder_within(x, by, within, fun = mean, sep = "___", ...) scale_x_reordered(..., labels = reorder_func, sep = deprecated()) scale_y_reordered(..., labels = reorder_func, sep = deprecated()) reorder_func(x, sep = "___") </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>Vector to reorder.</p> </td></tr> <tr valign="top"><td><code>by</code></td> <td> <p>Vector of the same length, to use for reordering.</p> </td></tr> <tr valign="top"><td><code>within</code></td> <td> <p>Vector or list of vectors of the same length that will later be used for faceting. A list of vectors will be used to facet within multiple variables.</p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>Function to perform within each subset to determine the resulting ordering. By default, mean.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>Separator to distinguish <code>by</code> and <code>within</code>. You may want to set this manually if ___ can exist within one of your labels.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>In <code>reorder_within</code> arguments passed on to <code><a href="../../stats/html/reorder.factor.html">reorder</a></code>. In the scale functions, extra arguments passed on to <code><a href="../../ggplot2/html/scale_x_discrete.html">scale_x_discrete</a></code> or <code><a href="../../ggplot2/html/scale_y_discrete.html">scale_y_discrete</a></code>.</p> </td></tr> <tr valign="top"><td><code>labels</code></td> <td> <p>Function to transform the labels of <code><a href="../../ggplot2/html/scale_discrete.html">ggplot2::scale_x_discrete()</a></code>, by default <code>reorder_func</code>.</p> </td></tr> </table> <h3>Source</h3> <p>"Ordering categories within ggplot2 Facets" by Tyler Rinker: <a href="https://trinkerrstuff.wordpress.com/2016/12/23/ordering-categories-within-ggplot2-facets/">https://trinkerrstuff.wordpress.com/2016/12/23/ordering-categories-within-ggplot2-facets/</a> </p> <h3>Examples</h3> <pre> library(tidyr) library(ggplot2) iris_gathered <- gather(iris, metric, value, -Species) # reordering doesn't work within each facet (see Sepal.Width): ggplot(iris_gathered, aes(reorder(Species, value), value)) + geom_boxplot() + facet_wrap(~ metric) # reorder_within and scale_x_reordered work. # (Note that you need to set scales = "free_x" in the facet) ggplot(iris_gathered, aes(reorder_within(Species, value, metric), value)) + geom_boxplot() + scale_x_reordered() + facet_wrap(~ metric, scales = "free_x") # to reorder within multiple variables, set within to the list of # facet variables. ggplot(mtcars, aes(reorder_within(carb, mpg, list(vs, am)), mpg)) + geom_boxplot() + scale_x_reordered() + facet_wrap(vs ~ am, scales = "free_x") </pre> <hr /><div style="text-align: center;">[Package <em>tidytext</em> version 0.3.4 <a href="00Index.html">Index</a>]</div> </body></html>