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")