Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has little to do with <strong>ggplot</strong>, but is instead a question about generating an ordering of variables to use to reorder the levels of a factor. Here is your data, implemented using the various functions to better effect:</p> <pre><code>set.seed(1234) df2 &lt;- data.frame(year = rep(2006:2007), variable = rep(c("VX","VB","VZ","VD"), each = 2), value = runif(8, 5,10), vartype = rep(c("TA","TB"), each = 4)) </code></pre> <p>Note that this way <code>variable</code> and <code>vartype</code> are factors. If they aren't factors, <code>ggplot()</code> will coerce them and then you get left with alphabetical ordering. I have said this before and will no doubt say it again; get your data into the correct format <strong>first</strong> <em>before</em> you start plotting / doing data analysis.</p> <p>You want the following ordering:</p> <pre><code>&gt; with(df2, order(vartype, variable)) [1] 3 4 1 2 7 8 5 6 </code></pre> <p>where you should note that we get the ordering by <code>vartype</code> first and only then by <code>variable</code> <em>within</em> the levels of <code>vartype</code>. If we use this to reorder the levels of <code>variable</code> we get:</p> <pre><code>&gt; with(df2, reorder(variable, order(vartype, variable))) [1] VX VX VB VB VZ VZ VD VD attr(,"scores") VB VD VX VZ 1.5 5.5 3.5 7.5 Levels: VB VX VD VZ </code></pre> <p>(ignore the <code>attr(,"scores")</code> bit and focus on the Levels). This has the right ordering, but <code>ggplot()</code> will draw them bottom to top and you wanted top to bottom. I'm not sufficiently familiar with <code>ggplot()</code> to know if this can be controlled, so we will also need to reverse the ordering using <code>decreasing = TRUE</code> in the call to <code>order()</code>.</p> <p>Putting this all together we have:</p> <pre><code>## reorder `variable` on `variable` within `vartype` df3 &lt;- transform(df2, variable = reorder(variable, order(vartype, variable, decreasing = TRUE))) </code></pre> <p>Which when used with your plotting code:</p> <pre><code>ggplot(df3, aes(x=variable, y=value, fill=vartype)) + geom_bar() + facet_grid(. ~ year) + coord_flip() </code></pre> <p>produces this:</p> <p><img src="https://i.stack.imgur.com/8lKV6.png" alt="reordered barplot"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload