ggplot堆积条重新排序图例是因子中定义的

时间:2016-07-05 06:56:34

标签: r ggplot2

我希望我的填充区域的排序方式与我的传奇相同。系数Antwort已正确排序,图例中的颜色正确无误。但是,图表中的顺序和颜色完全是混乱的。显然,条形按字母顺序重新排序。 enter image description here

数据集:

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)

这是我的代码:

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

你有提示我做错了吗?

更新

我没有找到使用关卡的解决方案。显然,基于字符串的级别每次都按字母顺序重新排序。我的解决方案是蛮力。我使用整数作为填充因子并使用scale_y_discrete(labels=five,breaks=NULL)手动分配标签。

暗示所有必须使用Lickert秤的人。 R的HH包为这种基于整数的标度提供了快速而美观的可视化。

0 个答案:

没有答案