R : ggplot geom_bar 채우기 순서가 레벨을 따르지 않음

조르디 비달

누적 막대 차트를 만들려고하는데 채우기 순서가 설정된 수준을 따르지 않습니다. 이것은 만족도 조사 보고서이므로 막대 채우기의 특정 순서가 있어야합니다.

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

막대가 레벨 대신 알파벳순으로 채워지는 것을 볼 수 있습니다. 나는 orderggplot aes에서 사용하려고 시도 했으며이 문제를 해결했지만 geom_text가 잘못 정렬되어 문제가 발생합니다.

당신의 도움을 주셔서 감사합니다!

대장장이

가장 쉬운 수정 방법은 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()) 

여기에 이미지 설명 입력

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

ggplot2 geom_bar 채우기 미학이 변경되지 않음

분류에서Dev

ggplot geom_bar () 플롯에서 막대를 채색하지 않음

분류에서Dev

ggplot geom_bar에서 변수 채우기 순서 변경

분류에서Dev

geom_bar ggplot2에서 색상을 변경하는 방법, IFelse가 작동하지 않음

분류에서Dev

R ggplot2 : 막대 내부의 레이블, 쌓인 geom_bar 없음

분류에서Dev

R 순서 geom_bar는 한 수준을 기준으로합니다.

분류에서Dev

ggplot : geom_bar 스택 순서 및 레이블

분류에서Dev

ggplot geom_bar 조건에 따라 별도의 색상으로 연속 색상 채우기

분류에서Dev

ggplot의 geom_bar에서 가로 막대 색상 지정

분류에서Dev

R / ggplot2를 사용하여 geom_bar () 내부의 개별 %에 레이블 추가

분류에서Dev

내가 원하지 않을 때 채우기를 추가하는 R ggplot2 선 그래프

분류에서Dev

ggplot2 geom_bar가 기본 정렬이 아닌 요인 수준으로 축을 그리는 방법

분류에서Dev

R-geom_bar가 올바른 값을 생성하지 않음

분류에서Dev

R에서 ggplot / geom_bar를 사용하여 막대 위에 데이터 세트의 사용자 지정 레이블을 추가하는 방법은 무엇입니까?

분류에서Dev

R : geom_bar에서 바 레이블 변경

분류에서Dev

ggplot : 막대가 많은 geom_bar

분류에서Dev

음수 값을 추가하지 않고 R ggplot2 geom_smooth

분류에서Dev

aws cli가 기본 리전 구성을 따르지 않음

분류에서Dev

JavaFX FliteredList가 기본 목록의 변경을 따르지 않음

분류에서Dev

ggplot geom_bar () 선 종류 및 채우기로 사용자 정의

분류에서Dev

geom_bar를 사용하여 ggplot2에 표시되지 않는 플롯

분류에서Dev

총 누적 막대 값으로 ggplot geom_bar 레이블 지정

분류에서Dev

R : 비닝 된 geom_bar에 대한 ggplot2 geom_text 데이터 레이블

분류에서Dev

모든 값을 표시하지 않는 geom_bar

분류에서Dev

캔버스보기가 상대 레이아웃 (XAML)을 채우지 않음

분류에서Dev

R ggplot geom_bar : 등고선 / 경계를 동일하게 유지하면서 철근 내부의 투명도 변경

분류에서Dev

ggplot : 각 독립 채우기를 겹치지 않고 geom_area () 또는 이와 유사한 세 가지 geom_line () 아래의 영역을 채우는 방법은 무엇입니까?

분류에서Dev

인쇄 미리보기가 너비 이미지 / 색상을 따르지 않음

분류에서Dev

ggplot geom_bar에서 NA 값의 레이블 및 색상을 변경 하시겠습니까?

Related 관련 기사

  1. 1

    ggplot2 geom_bar 채우기 미학이 변경되지 않음

  2. 2

    ggplot geom_bar () 플롯에서 막대를 채색하지 않음

  3. 3

    ggplot geom_bar에서 변수 채우기 순서 변경

  4. 4

    geom_bar ggplot2에서 색상을 변경하는 방법, IFelse가 작동하지 않음

  5. 5

    R ggplot2 : 막대 내부의 레이블, 쌓인 geom_bar 없음

  6. 6

    R 순서 geom_bar는 한 수준을 기준으로합니다.

  7. 7

    ggplot : geom_bar 스택 순서 및 레이블

  8. 8

    ggplot geom_bar 조건에 따라 별도의 색상으로 연속 색상 채우기

  9. 9

    ggplot의 geom_bar에서 가로 막대 색상 지정

  10. 10

    R / ggplot2를 사용하여 geom_bar () 내부의 개별 %에 레이블 추가

  11. 11

    내가 원하지 않을 때 채우기를 추가하는 R ggplot2 선 그래프

  12. 12

    ggplot2 geom_bar가 기본 정렬이 아닌 요인 수준으로 축을 그리는 방법

  13. 13

    R-geom_bar가 올바른 값을 생성하지 않음

  14. 14

    R에서 ggplot / geom_bar를 사용하여 막대 위에 데이터 세트의 사용자 지정 레이블을 추가하는 방법은 무엇입니까?

  15. 15

    R : geom_bar에서 바 레이블 변경

  16. 16

    ggplot : 막대가 많은 geom_bar

  17. 17

    음수 값을 추가하지 않고 R ggplot2 geom_smooth

  18. 18

    aws cli가 기본 리전 구성을 따르지 않음

  19. 19

    JavaFX FliteredList가 기본 목록의 변경을 따르지 않음

  20. 20

    ggplot geom_bar () 선 종류 및 채우기로 사용자 정의

  21. 21

    geom_bar를 사용하여 ggplot2에 표시되지 않는 플롯

  22. 22

    총 누적 막대 값으로 ggplot geom_bar 레이블 지정

  23. 23

    R : 비닝 된 geom_bar에 대한 ggplot2 geom_text 데이터 레이블

  24. 24

    모든 값을 표시하지 않는 geom_bar

  25. 25

    캔버스보기가 상대 레이아웃 (XAML)을 채우지 않음

  26. 26

    R ggplot geom_bar : 등고선 / 경계를 동일하게 유지하면서 철근 내부의 투명도 변경

  27. 27

    ggplot : 각 독립 채우기를 겹치지 않고 geom_area () 또는 이와 유사한 세 가지 geom_line () 아래의 영역을 채우는 방법은 무엇입니까?

  28. 28

    인쇄 미리보기가 너비 이미지 / 색상을 따르지 않음

  29. 29

    ggplot geom_bar에서 NA 값의 레이블 및 색상을 변경 하시겠습니까?

뜨겁다태그

보관