【发布时间】:2016-03-30 10:11:39
【问题描述】:
我正在尝试创建连续的条形图
Creating a stacked bar chart centered on zero using ggplot
但是,即使我已将填充变量指定为有序因子,我的条形图中填充水平的顺序似乎也被忽略了?
MWE
# Illustrate problem with factors being ignored
mydata <- read.csv(textConnection(
"Attribute, Score, Percent
A1, Poor, 10
A1, Below Avg, 20
A1, Above Avg, 20
A1, Excellent, 50
A2, Poor, 20
A2, Below Avg, 20
A2, Above Avg, 20
A2, Excellent, 40"), as.is=TRUE, strip.white=TRUE, header=TRUE)
mydata$Score <- factor(mydata$Score,
levels=c('Poor','Below Avg','Above Avg','Excellent'), order=TRUE)
str(mydata)
myplot <- ggplot(data=mydata, aes(x=Attribute))+
geom_bar(data=subset(mydata,
as.character(mydata$Score) %in% c("Poor","Below Avg")),
aes(y= -Percent, fill=Score), stat='identity')+
geom_bar(data=subset(mydata,
!as.character(mydata$Score) %in% c("Poor","Below Avg")),
aes(y= Percent, fill=Score), stat='identity')+
geom_hline( yintercept=0, size=2, color="red")+
coord_flip()
myplot
给予
请注意,填充的级别是按字母顺序排序的,而不是根据排序因子。
谢谢 卡尔·施瓦茨
【问题讨论】: