R:ggplot geom_bar填充订单不在以下级别

时间:2015-08-28 15:21:15

标签: r ggplot2

我正在尝试构建堆叠条形图,但填充顺序不符合设置的级别。这是一份满意度调查报告,因此需要有一个特定的条形填充顺序。

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

这是ggplot代码:

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

您将看到按字母顺序填充条形而不是水平。我曾尝试在ggplot aes中使用order并且它解决了这个问题,但是当它们变得不对齐时会产生geom_text的问题。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为最简单的解决方法是计算Position变量以匹配您想要条形的顺序。因为该位置可能基于累积总和,如果您未在组内设置顺序那么你的文本将不会与堆栈的顺序相匹配。

这里我将使用 dplyr 中的函数计算位置。我使用arrange按您已经设置的因子级别排序。

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

使用新数据集,填充按value顺序排列,标签和堆栈位于正确的位置(看起来与我的眼睛匹配)。

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