0

I want my filled areas ordered in the same way as my legend. The factor Antwort is already ordered correctly and colors in the legend are correct. However, the order and the colors in the graph are completely jumbled. Apparently the bars get reordered in alphabetical order. enter image description here

dataset:

gdata <- structure(list(Antwort=c("Eher zutreffend","Trifft eher nicht 
zu","Trifft überhaupt nicht zu","Trifft voll zu","Weder noch","Eher 
zutreffend","Trifft eher nicht zu","Trifft überhaupt nicht zu","Trifft 
voll zu","Weder noch","Eher zutreffend","Trifft eher nicht zu","Trifft 
überhaupt nicht zu","Trifft voll zu","Weder noch","Eher  
zutreffend","Trifft eher nicht zu","Trifft überhaupt nicht zu","Trifft 
voll zu","Weder noch","Eher zutreffend","Trifft eher nicht zu","Trifft 
überhaupt nicht zu","Trifft voll zu","Weder 
noch"),val=c(307,441,55,54,195,481,290,12,33,236,432,299,1,46,274,405,357,
27,53,210,506,51,20,399,76),label=c("Extraversion","Extraversion",
"Extraversion","Extraversion","Extraversion","Agreeableness",
"Agreeableness","Agreeableness","Agreeableness","Agreeableness",
"Conscientiousness","Conscientiousness","Conscientiousness",
"Conscientiousness","Conscientiousness","Neurotic","Neurotic",
"Neurotic","Neurotic","Neurotic","Openness","Openness","Openness",
"Openness","Openness")),.Names=c("Antwort","val","label"),
class="data.frame",row.names=1:25)

And here is my code:

five = c('Trifft voll zu', 'Eher zutreffend', 'Weder noch', 'Trifft eher nicht zu', 'Trifft überhaupt nicht zu')
gdata$Antwort <- factor(gdata$Antwort,levels=five)

ggplot(data=gdata, aes(x=label, y=val)) +
geom_bar(stat="identity",colour="black",position="stack",aes(fill=Antwort)) +
 scale_fill_manual(values=c("#4169e1","#808080","#d3d3d3","#808080","#ff4500")) +
 coord_flip()

Do you have a hint what I do wrong?

Update:

I didn't find a solution using levels. Apparently the string based levels got reordered alphabetically for the fill every time. My solution was brute force. I used integers as factors for fill and assigned labels manually using scale_y_discrete(labels=five,breaks=NULL).

A hint to all who have to work with Lickert scales. the HH package for R provides quick and beautiful visualizations for such integer based scales.

Sahir Moosvi
  • 549
  • 2
  • 21
cataclysmic
  • 187
  • 7
  • Your code is not reproducible. I will suggest that you should try ordered factor. Then it should give the colors in the order in which you order the factor. – Kumar Manglam Jul 05 '16 at 07:04
  • I thought the `factor(gdata$Antwort, levels=five)` does the reordering? – cataclysmic Jul 05 '16 at 07:07
  • Any help in this post? http://stackoverflow.com/questions/8186436/order-stacked-bar-graph-in-ggplot – GPierre Jul 05 '16 at 07:17
  • For making ordered factor, you need to use ordered argument e.g. `factor(gdata$Antwort, levels=five, ordered = TRUE)` – Kumar Manglam Jul 05 '16 at 07:17
  • I tried including `ordered=TRUE` but it didn't improve. How can I make the code reproducible for you? – cataclysmic Jul 05 '16 at 07:25
  • It's not easy to help you as you did not provided some data. But you could try to simply add a `scale_fill_discrete(breaks = levels(gdata$Antwort))` – Roman Jul 05 '16 at 07:32
  • I added the dataset for reproduction. I tried `ordered=TRUE` but it sadly didn't help. – cataclysmic Jul 05 '16 at 08:03
  • 1
    The easiest thing to do is to order the dataset in the order you want the bars as shown [here](http://stackoverflow.com/a/34637703/2461552) – aosmith Jul 05 '16 at 18:26

0 Answers0