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.
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.