0

Could anyone explain the inconsistent behavior below?

Sample data:

structure(list(a11 = c(2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
2L, 2L, 1L, 1L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 
1L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 5L, 3L, 3L, 3L, 5L, 6L, 3L, 
3L, 3L, 1L, 3L, 2L, 3L, 1L, 3L), variable = structure(c(3L, 2L, 
1L, 2L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 1L, 4L, 3L, 3L, 1L, 1L, 1L, 
2L, 1L, 3L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 1L, 1L, 
2L, 2L, 4L, 2L, 2L, 1L, 4L, 1L, 2L, 1L, 3L, 3L, 1L, 4L, 4L, 3L
), .Label = c("e15_1", "e15_2", "e15_4", "e15_5"), class = "factor"), 
    value = c(0, 85, 0, 0, 5, 10, 50, 20, 100, 0, 0, 0, 40, 50, 
    70, 0, 2, 0, 100, 0, 80, 20, 80, 0, 50, 20, 22, 0, 10, 25, 
    36, 0, 0, 0, 25, 25, 0, 10, 10, 0, 50, 5, 100, 0, 100, 40, 
    80, 0, 50, 50)), .Names = c("a11", "variable", "value"), row.names = c(13984L, 
5521L, 587L, 9204L, 15516L, 18568L, 11912L, 14028L, 14125L, 18348L, 
14085L, 4803L, 17191L, 10902L, 11006L, 4420L, 3996L, 3634L, 5339L, 
3428L, 10279L, 16692L, 13879L, 3778L, 3602L, 8917L, 8283L, 18522L, 
18690L, 18160L, 6897L, 14611L, 1540L, 1973L, 7695L, 9152L, 17636L, 
5909L, 9590L, 420L, 17516L, 617L, 5008L, 4162L, 10631L, 12311L, 
3534L, 17533L, 18206L, 11632L), class = "data.frame")

Code (notice the use of group in aes and how it affects the order in the middle bar):

# NOT using group in aes give inconsistent order
p1 <- ggplot(data = tmp) + 
  stat_summary(aes(x = factor(a11), y = value, fill = variable),
               fun.y = mean, geom = "bar", position = "stack")
# Using group in aes gives consistent order
p2 <- ggplot(data = tmp) +
  stat_summary(aes(x = factor(a11), y = value, 
                   fill = variable, group = variable),
               fun.y = mean, geom = "bar", position = "stack")
grid.arrange(p1, p2)

enter image description here

Heisenberg
  • 8,386
  • 12
  • 53
  • 102
  • I _think_ you may find [this Q&A](http://stackoverflow.com/questions/34716245/ggplot2-changing-the-order-of-stacks-on-a-bar-graph/) useful. – Henrik Jan 25 '16 at 21:46
  • Could downvoter please explain? The question has sample data, example code, and clear, reproducible result. – Heisenberg Jan 27 '16 at 07:03
  • Because you pinged me, I assume you believe I'm downvoted. I did not. Your post does indeed include what is expected from a good post (in stark contrast to the flood of 'plz-send-codez' questions on SO). I hope you found the link I suggested helpful. Good luck! – Henrik Jan 27 '16 at 07:13
  • And regarding explaining downvotes, I am sure you are aware of the discussions on Meta on this topic. – Henrik Jan 27 '16 at 07:15
  • @Henrik Could you please point me to the Meta discussion? PS: The comment wasn't aimed at you, I just don't know where else to ask for feedback. PS 2: The link you mentioned is the canonical way to reorder bars (by specifying the factor levels), but even if I do so, the order is still inconsistent without `group` aesthetics. – Heisenberg Jan 27 '16 at 07:17
  • 1
    Just search for "explaining downvotes" on Meta. Regarding the link, I _think_ they made a point that it is not "only" the factor levels which need to be considered, but also the actual order of the data in data set. I haven't read it very thoroughly though - hence my careful wording ("I _think_ you may") when suggesting it - but you might have a closer look. If I knew it is was an _obvious_ duplicate, I would have dupehammered your post right away ;) Cheers. – Henrik Jan 27 '16 at 07:23

0 Answers0