0
votes

I am trying to build a stacked barchart but the fill order does not follow the levels set. This is a report of a satisfaction survey, so there needs to be a specific order of the bar fill.

> head(dput(table.merged))
structure(list(value = structure(c(2L, 3L, 4L, 1L, 5L, 2L, 3L, 
4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 
1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 
5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 
2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L, 2L, 
3L, 4L, 1L, 5L, 2L, 3L, 4L, 1L, 5L), .Label = c("Very dissatisfied", 
"Dissatisfied", "Neutral", "Satisfied", "Very satisfied"), class = "factor"), 
    site = c("Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Barcelona", "Barcelona", "Barcelona", 
    "Barcelona", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon", 
    "Lisbon", "Lisbon", "Lisbon", "Lisbon", "Lisbon"), variable = c("Is the TMS Team approachable ", 
    "Is the TMS Team approachable ", "Is the TMS Team approachable ", 
    "Is the TMS Team approachable ", "Is the TMS Team approachable ", 
    "They give you the chance to listen to your call ", "They give you the chance to listen to your call ", 
    "They give you the chance to listen to your call ", "They give you the chance to listen to your call ", 
    "They give you the chance to listen to your call ", "They fairly evaluate your calls ", 
    "They fairly evaluate your calls ", "They fairly evaluate your calls ", 
    "They fairly evaluate your calls ", "They fairly evaluate your calls ", 
    "They explain in detail the result of your call ", "They explain in detail the result of your call ", 
    "They explain in detail the result of your call ", "They explain in detail the result of your call ", 
    "They explain in detail the result of your call ", "They are knowledgeable about H2H ", 
    "They are knowledgeable about H2H ", "They are knowledgeable about H2H ", 
    "They are knowledgeable about H2H ", "They are knowledgeable about H2H ", 
    "They reinforce the 3As approach and importance ", "They reinforce the 3As approach and importance ", 
    "They reinforce the 3As approach and importance ", "They reinforce the 3As approach and importance ", 
    "They reinforce the 3As approach and importance ", "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "Is the TMS Team approachable ", "Is the TMS Team approachable ", 
    "Is the TMS Team approachable ", "Is the TMS Team approachable ", 
    "Is the TMS Team approachable ", "They give you the chance to listen to your call ", 
    "They give you the chance to listen to your call ", "They give you the chance to listen to your call ", 
    "They give you the chance to listen to your call ", "They give you the chance to listen to your call ", 
    "They fairly evaluate your calls ", "They fairly evaluate your calls ", 
    "They fairly evaluate your calls ", "They fairly evaluate your calls ", 
    "They fairly evaluate your calls ", "They explain in detail the result of your call ", 
    "They explain in detail the result of your call ", "They explain in detail the result of your call ", 
    "They explain in detail the result of your call ", "They explain in detail the result of your call ", 
    "They are knowledgeable about H2H ", "They are knowledgeable about H2H ", 
    "They are knowledgeable about H2H ", "They are knowledgeable about H2H ", 
    "They are knowledgeable about H2H ", "They reinforce the 3As approach and importance ", 
    "They reinforce the 3As approach and importance ", "They reinforce the 3As approach and importance ", 
    "They reinforce the 3As approach and importance ", "They reinforce the 3As approach and importance ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "They give effective coaching to help you improve your NS results  including H2H and 3As ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  ", 
    "What is the overall satisfaction regarding the TMS Team  "
    ), score = c(0.0431034482758621, 0.129310344827586, 0.413793103448276, 
    0.0258620689655172, 0.387931034482759, 0.0431034482758621, 
    0.293103448275862, 0.310344827586207, 0.0603448275862069, 
    0.293103448275862, 0.0603448275862069, 0.198275862068966, 
    0.387931034482759, 0.0431034482758621, 0.310344827586207, 
    0.0172413793103448, 0.155172413793103, 0.413793103448276, 
    0.0258620689655172, 0.387931034482759, 0.0258620689655172, 
    0.103448275862069, 0.379310344827586, 0.0258620689655172, 
    0.46551724137931, 0.0172413793103448, 0.0948275862068965, 
    0.336206896551724, 0.0258620689655172, 0.525862068965517, 
    0.0344827586206897, 0.146551724137931, 0.318965517241379, 
    0.0344827586206897, 0.46551724137931, 0.0344827586206897, 
    0.181034482758621, 0.336206896551724, 0.0258620689655172, 
    0.422413793103448, 0.0628930817610063, 0.270440251572327, 
    0.383647798742138, 0.0377358490566038, 0.245283018867925, 
    0.119496855345912, 0.364779874213836, 0.232704402515723, 
    0.10062893081761, 0.182389937106918, 0.0880503144654088, 
    0.19496855345912, 0.446540880503145, 0.0314465408805031, 
    0.238993710691824, 0.0691823899371069, 0.138364779874214, 
    0.452830188679245, 0.0251572327044025, 0.314465408805031, 
    0.0314465408805031, 0.182389937106918, 0.427672955974843, 
    0.0251572327044025, 0.333333333333333, 0.0377358490566038, 
    0.132075471698113, 0.427672955974843, 0.0125786163522013, 
    0.389937106918239, 0.0566037735849057, 0.207547169811321, 
    0.364779874213836, 0.050314465408805, 0.320754716981132, 
    0.0628930817610063, 0.19496855345912, 0.408805031446541, 
    0.0440251572327044, 0.289308176100629), Position = c(0.021551724137931, 
    0.107758620689655, 0.379310344827586, 0.599137931034483, 
    0.806034482758621, 0.021551724137931, 0.189655172413793, 
    0.491379310344828, 0.676724137931034, 0.853448275862069, 
    0.0301724137931034, 0.15948275862069, 0.452586206896552, 
    0.668103448275862, 0.844827586206897, 0.00862068965517241, 
    0.0948275862068966, 0.379310344827586, 0.599137931034483, 
    0.806034482758621, 0.0129310344827586, 0.0775862068965517, 
    0.318965517241379, 0.521551724137931, 0.767241379310345, 
    0.00862068965517241, 0.0646551724137931, 0.280172413793103, 
    0.461206896551724, 0.737068965517241, 0.0172413793103448, 
    0.107758620689655, 0.34051724137931, 0.517241379310345, 0.767241379310345, 
    0.0172413793103448, 0.125, 0.383620689655172, 0.564655172413793, 
    0.788793103448276, 0.0314465408805031, 0.19811320754717, 
    0.525157232704403, 0.735849056603774, 0.877358490566038, 
    0.059748427672956, 0.30188679245283, 0.60062893081761, 0.767295597484277, 
    0.908805031446541, 0.0440251572327044, 0.185534591194969, 
    0.506289308176101, 0.745283018867925, 0.880503144654088, 
    0.0345911949685535, 0.138364779874214, 0.433962264150943, 
    0.672955974842767, 0.842767295597484, 0.0157232704402516, 
    0.122641509433962, 0.427672955974843, 0.654088050314465, 
    0.833333333333333, 0.0188679245283019, 0.10377358490566, 
    0.383647798742138, 0.60377358490566, 0.805031446540881, 0.0283018867924528, 
    0.160377358490566, 0.446540880503145, 0.654088050314465, 
    0.839622641509434, 0.0314465408805031, 0.160377358490566, 
    0.462264150943396, 0.688679245283019, 0.855345911949686), 
    variable2 = c("Is the TMS Team\napproachable", "Is the TMS Team\napproachable", 
    "Is the TMS Team\napproachable", "Is the TMS Team\napproachable", 
    "Is the TMS Team\napproachable", "They give you\nthe chance to\nlisten to your\ncall", 
    "They give you\nthe chance to\nlisten to your\ncall", "They give you\nthe chance to\nlisten to your\ncall", 
    "They give you\nthe chance to\nlisten to your\ncall", "They give you\nthe chance to\nlisten to your\ncall", 
    "They fairly\nevaluate your\ncalls", "They fairly\nevaluate your\ncalls", 
    "They fairly\nevaluate your\ncalls", "They fairly\nevaluate your\ncalls", 
    "They fairly\nevaluate your\ncalls", "They explain\nin detail the\nresult of your\ncall", 
    "They explain\nin detail the\nresult of your\ncall", "They explain\nin detail the\nresult of your\ncall", 
    "They explain\nin detail the\nresult of your\ncall", "They explain\nin detail the\nresult of your\ncall", 
    "They are\nknowledgeable\nabout H2H", "They are\nknowledgeable\nabout H2H", 
    "They are\nknowledgeable\nabout H2H", "They are\nknowledgeable\nabout H2H", 
    "They are\nknowledgeable\nabout H2H", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They reinforce\nthe 3As\napproach and\nimportance", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They reinforce\nthe 3As\napproach and\nimportance", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "Is the TMS Team\napproachable", "Is the TMS Team\napproachable", 
    "Is the TMS Team\napproachable", "Is the TMS Team\napproachable", 
    "Is the TMS Team\napproachable", "They give you\nthe chance to\nlisten to your\ncall", 
    "They give you\nthe chance to\nlisten to your\ncall", "They give you\nthe chance to\nlisten to your\ncall", 
    "They give you\nthe chance to\nlisten to your\ncall", "They give you\nthe chance to\nlisten to your\ncall", 
    "They fairly\nevaluate your\ncalls", "They fairly\nevaluate your\ncalls", 
    "They fairly\nevaluate your\ncalls", "They fairly\nevaluate your\ncalls", 
    "They fairly\nevaluate your\ncalls", "They explain\nin detail the\nresult of your\ncall", 
    "They explain\nin detail the\nresult of your\ncall", "They explain\nin detail the\nresult of your\ncall", 
    "They explain\nin detail the\nresult of your\ncall", "They explain\nin detail the\nresult of your\ncall", 
    "They are\nknowledgeable\nabout H2H", "They are\nknowledgeable\nabout H2H", 
    "They are\nknowledgeable\nabout H2H", "They are\nknowledgeable\nabout H2H", 
    "They are\nknowledgeable\nabout H2H", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They reinforce\nthe 3As\napproach and\nimportance", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They reinforce\nthe 3As\napproach and\nimportance", "They reinforce\nthe 3As\napproach and\nimportance", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "They give\neffective\ncoaching\nto help you\nimprove your\nNS results\nincluding H2H\nand 3As", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team", 
    "What is\nthe overall\nsatisfaction\nregarding the\nTMS Team"
    )), .Names = c("value", "site", "variable", "score", "Position", 
"variable2"), row.names = c(NA, 80L), vars = list(variable), labels = structure(list(
    variable = structure(1:8, .Label = c("Is.the.TMS.Team.approachable.", 
    "They.give.you.the.chance.to.listen.to.your.call.", "They.fairly.evaluate.your.calls.", 
    "They.explain.in.detail.the.result.of.your.call.", "They.are.knowledgeable.about.H2H.", 
    "They.reinforce.the.3As.approach.and.importance.", "They.give.effective.coaching.to.help.you.improve.your.NS.results..including.H2H.and.3As.", 
    "What.is.the.overall.satisfaction.regarding.the.TMS.Team.."
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-8L), .Names = "variable", vars = list(variable)), indices = list(
    0:4, 5:9, 10:14, 15:19, 20:24, 25:29, 30:34, 35:39), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

This is the ggplot piece of code:

ggplot(table.merged,aes(variable2,score,fill=value))+
  geom_bar(stat="identity",alpha=0.6,colour="grey60")+
  geom_text(aes(label=round(score,2),y=table.merged$Position),colour="white")+
  facet_wrap(~site)+
  scale_fill_manual(values=c("darkred","red","orange","green","darkgreen"))+
  labs(title="General satisfaction with TMS", y="Percentage",x="")+
  theme(plot.title=element_text(vjust=1),
        legend.position="top",
        legend.title=element_blank()) 

You will see that the bars are filled alphabetically instead of the levels. I have tried to use order in ggplot aes and it solves this issue, but then it creates problems with the geom_text as they become misaligned.

Thanks for your help!

1
Before the melt, did you trying something like df$level <- factor(df$level, levels = c("dissatisfied", "satisfied", etc) to set the levels in the order you want?lawyeR
Hi, yes. "value" column is factored with the correct leveling. The code is the following: sat.factors <- c("Very dissatisfied","Dissatisfied","Neutral","Satisfied","Very satisfied") table.merged$value <- factor(table.merged$value,levels=c(sat.factors))Jordi Vidal
It looks like you may have calculated the positions via cumsum before setting your factor order and arranging the dataset. Try recalculating your positions for each site/variable2 combo while arranging things by your value variable.aosmith
Hi, thanks for the suggestion. I had a look at the code, and first I set the factor, and then calculate the position for each site/variable. The problem isn't the cumsum, it is the order fill of ggplot. Granted that if I can re-work geom_text to accomodate for order in aes, that would be great as well. Just to be clear: levels are correctly set. If I look into factor(values), the levels are correct. It is just that ggplot ignores them when filling the barsJordi Vidal

1 Answers

0
votes

I think the easiest fix is to calculate the Position variable to match the order you want the bars in. Because the position is likely based on a cumulative sum, if you don't set the order within your groups then your text won't match the order of the stacks.

Here I'll calculate the positions with functions from dplyr. I use arrange to order by the factor levels that you've already set.

library(dplyr)
table.merged2 = table.merged %>% 
    group_by(site, variable2) %>% 
    arrange(value) %>%
    mutate(position2 = cumsum(score) - (0.5 * score))

Using the new dataset, the fill is in order by value and the labels and stacks are in the correct positions (and seem to match, by my eye).

ggplot(table.merged2, aes(variable2, score, fill=value)) +
    geom_bar(stat="identity",alpha=0.6,colour="grey60") +
    geom_text(aes(label=round(score,2), y=position2), colour="white") +
    facet_wrap(~site) +
    scale_fill_manual(values=c("darkred","red","orange","green","darkgreen")) +
    labs(title="General satisfaction with TMS", y="Percentage", x="") +
    theme(plot.title=element_text(vjust=1),
                legend.position="top",
                legend.title=element_blank()) 

enter image description here