2

I want to plot a result of some dummy data (see below). I want to use a (stacked bar plot and ggplot2) The plot should differentiate between the Profiles (facet_grid(Profile~.)) and show the results of the Answer values in a stacked bar chart, by question category. I used the following code:

  ggplot(profile_results, aes(x=Question, fill=Answer, y=Value)) + 
    geom_bar( stat="identity") + 
    scale_fill_brewer(palette = "Set1") + 
    coord_flip() + 
    facet_grid(Profile~.)

However, the result is weirdly off. profle_analysis_result

I also tried to use factors for my Answer categories my_levels = c("Strongly Disagree", "Disagree", "Undecided", "Agree", "Strongly Agree") profile_results$Answer <- factor(profile_results$Answer, levels=my_levels)

without any result. I also used the aes() order option (order=Answer) without any success.

What am I missing?

===EDIT 1 Reordering the results (as suggested by @Rohit) did not help either.

my_levels =  c("Strongly Disagree", "Disagree", "Undecided", "Agree", "Strongly Agree")
profile_results$Answer <- ordered(profile_results$Answer, my_levels)
profile_results= profile_results[order(profile_results$Answer),]

ggplot(profile_results, aes(x=Question, fill=Answer, y=Value, ordered=TRUE)) + 
  geom_bar( stat="identity") + 
  scale_fill_brewer(palette = "Set1") + 
  coord_flip() + 
  facet_grid(Profile~.)

second_try

===EDIT 2

Please finde the dput here https://pastebin.com/igFiaBTR

(shortend)

 Profile   Question            Answer              Value
2  technical     expect Strongly Disagree  0.166666666666667
6  technical     expect    Strongly Agree  0.166666666666667
7  technical       help Strongly Disagree  0.166666666666667
11 technical       help    Strongly Agree  0.166666666666667
12 technical understand Strongly Disagree  0.166666666666667
17 technical      clear Strongly Disagree  0.333333333333333
18 technical      clear          Disagree               0.25
19 technical      clear         Undecided  0.166666666666667
20 technical      clear             Agree 0.0833333333333333
21 technical      clear    Strongly Agree  0.166666666666667
22  Tracking     expect Strongly Disagree 0.0833333333333333
23  Tracking     expect          Disagree  0.166666666666667
24  Tracking     expect         Undecided 0.0833333333333333
49  Interest       help         Undecided 0.0833333333333333
50  Interest       help             Agree  0.333333333333333
51  Interest       help    Strongly Agree 0.0833333333333333
52  Interest understand Strongly Disagree  0.166666666666667
53  Interest understand          Disagree                  0
54  Interest understand         Undecided 0.0833333333333333
55  Interest understand             Agree  0.416666666666667
56  Interest understand    Strongly Agree  0.333333333333333
57  Interest      clear Strongly Disagree  0.166666666666667
58  Interest      clear          Disagree  0.166666666666667
59  Interest      clear         Undecided               0.25
60  Interest      clear             Agree  0.333333333333333
61  Interest      clear    Strongly Agree 0.0833333333333333

2 Answers2

3

It's because your Value variable is of type character. Converting to numeric first will allow you to order the bars using your factor levels.

profile_results$Value <- as.numeric(profile_results$Value)
my_levels =  c("Strongly Disagree", "Disagree", "Undecided", "Agree", "Strongly Agree")
profile_results$Answer <- factor(profile_results$Answer, my_levels)


ggplot(profile_results, aes(x=Question, fill=Answer, y=Value)) + 
  geom_bar( stat="identity") + 
  scale_fill_brewer(palette = "Set1") + 
  coord_flip() + 
  facet_grid(Profile~.)
Chris
  • 3,836
  • 1
  • 16
  • 34
0

This seems to solve your problem:

ggplot(profile_results, aes(x=Question, group=Answer, fill=Answer, y=Value)) + 
    geom_bar( stat="identity") + 
    scale_fill_brewer(palette = "Set1") + 
    coord_flip() + 
    facet_grid(Profile~.)
Erich Neuwirth
  • 943
  • 7
  • 13