change the order in geom_bar of the stacked variables

100 views
Skip to first unread message

Alessandra Carioli

unread,
Jan 26, 2016, 2:00:05 PM1/26/16
to ggplot2

Dear ggplotters,

I am trying to change the stacking order of categories so that “Recent change in preferences” appears plotted last, after “observed”, “competing preferences” , “waiting time”, and “strict involuntary factors”

I have created an ordering variable, numV and added order in the aes but it does not work:

numV = factor( dt$Type,  c(“Observed”, "Competing Preferences", ”Waiting time", "Strict Involuntary factors", "Recent change in Preferences"))


ggplot( subset( dt, Variable == TFR1), aes ( reorder( Country, rev( order )), fill = numV, order= Type, value))+
  geom_bar( stat= "identity") +
  scale_fill_manual(values=c( "brown""lightblue""purple”, "darkgreen", "green"),breaks=c("Observed", "Competing Preferences", "Waiting time", "Strict Involuntary factors", "Recent change in Preferences" ))+
  coord_flip()+
  geom_hline( yintercept= 1, col= "red", lty=2)
  

dt <- structure(list(Country = structure(c(1L, 2L, 8L, 11L, 6L, 10L, 
3L, 4L, 5L, 7L, 9L, 12L, 1L, 2L, 8L, 11L, 6L, 10L, 3L, 4L, 5L, 
7L, 9L, 12L, 1L, 2L, 8L, 11L, 6L, 10L, 3L, 4L, 5L, 7L, 9L, 12L, 
1L, 2L, 8L, 11L, 6L, 10L, 3L, 4L, 5L, 7L, 9L, 12L, 1L, 2L, 8L, 
11L, 6L, 10L, 3L, 4L, 5L, 7L, 9L, 12L), .Label = c("Austria", 
"Belgium", "Bulgaria", "Czech Rep", "Estonia", "Finland", "Hungary", 
"Italy", "Lithuania", "Norway", "Portugal", "Slovenia"), class = "factor"), 
    Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L), .Label = c("Competing Preferences", 
    "Observed", "Recent change in Preferences", "Strict Involuntary factors", 
    "Waiting time"), class = "factor"), Variable = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("PPR", 
    "TFR", "TFR1"), class = "factor"), value = c(0.915019618935318, 
    0.983361531957373, 0.914760769151969, 0.859972469668077, 
    0.844763634128951, 0.9304363613935, 0.871577019929315, 0.973252000207427, 
    0.751675755207165, 0.989007326062785, 0.883086301186579, 
    0.86635016841834, 0, 0.007408699, 0, 0, 0.02554987, 0.011588498, 
    0, 0, 0, 0.025129468, 0, 0, 0.069921961, 0, 0.046661336, 
    0.125630417, 0, 0, 0.121056141, 0.016275558, 0.226832447, 
    0, 0.108421811, 0.121703024, 0.008784752, 0.010205031, 0.019966701, 
    -0.000427248, 0.005958654, 0.005720579, 0.001671895, 0.006065908, 
    0.010108353, 0.003915961, 0.001104456, 6.96476e-05, 0.006273667, 
    0.006433437, 0.018611195, 0.014824362, 0.149277712, 0.063843059, 
    0.005694944, 0.004406534, 0.011383445, -0.018052755, 0.007387433, 
    0.01187716), order = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), numV = c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L)), .Names = c("Country", 
"Type", "Variable", "value", "order", "numV"), row.names = c(25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 61L, 62L, 
63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 97L, 98L, 99L, 
100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 133L, 134L, 
135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 169L, 
170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L
), class = "data.frame")

Gray, Wayne

unread,
Jan 26, 2016, 3:30:41 PM1/26/16
to Alessandra Carioli, ggplot2
Alessandra,

##  REORDERING LEVELS
levels(e2eye$DIRTIME)
[1] "EARLY"  "LATE"   "MIDDLE"

e2eye$DIRTIME <- factor(e2eye$DIRTIME, levels=c("EARLY", "MIDDLE", "LATE"))

levels(e2eye$DIRTIME)
[1] "EARLY"  "MIDDLE" “LATE"

Works well.

Wayne



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gray, Wayne

unread,
Jan 27, 2016, 9:50:58 PM1/27/16
to Alessandra Carioli, ggplot2
Alessandra,

I give up!!

I spent some time on your problem — starting by simplifying it to eliminate everything but what was causing your problem:

dt$Type <- factor(dt$Type, levels=c("Observed", "Competing Preferences", "Waiting time", "Strict Involuntary factors", "Recent change in Preferences"))

ggplot(dt, aes(x=Country, y=value, fill = Type)) +
geom_bar(stat="identity”)

So — this eliminates the coord_flip(), as well as reversing the ordering of “Country”, it also removes your assignment of color and breaks. It is pretty much the simplest stacked-barchart of these data and I am still not able to control the order in which the stacks stack.

Perhaps someone else can help us both out.

Wayne

BTW: you have two values that are < 0. Row 40 and row 58. You probably want to eliminate those.




On 16-Jan-27, at 04:03 , Alessandra Carioli <alessand...@gmail.com> wrote:

Hi Wayne,

it does reorder the levels in the legend but not in the graph. Any ideas how to do it?

Ale

ggplot(subset(dt,Variable=="TFR1"),aes(reorder(Country,rev(order)),fill=Type,value))+
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("lightblue", "brown", "green","darkgreen","purple"),breaks=c("Observed","Competing Preferences","Waiting time","Strict Involuntary factors","Recent change in Preferences"))+
  coord_flip()

Ben

unread,
Jan 29, 2016, 6:17:57 AM1/29/16
to ggplot2, alessand...@gmail.com
First, let me echo Wayne's comment--please SIMPLIFY reproducible examples as much as possible.

I think this is a long-standing bug (https://github.com/hadley/ggplot2/issues/721), but it can be worked around:

# using arrange from dplyr:
ggplot(arrange(dt, Type), aes(x=Country, y=value, fill = Type)) + geom_bar(stat='identity')

# using base R:
ggplot(dt[order(dt$Type), ], aes(x=Country, y=value, fill = Type)) + geom_bar(stat='identity')

Ben
Reply all
Reply to author
Forward
0 new messages