让您的代码运行存在一些问题。首先,我认为,您错过了从您显示的数据中融合数据的部分c.data
:
c.data <- melt(dat, id.var="Subject")
其次,你subject
应该是Subject
:
第三,既然你将x
和y
论据都传递给美学,你应该这样做stat = "identity"
。否则,您将收到警告,因为默认参数是stat = "bin"
.
最后,要以您需要的方式对绘图进行排序,您必须Subject
相应地设置因子水平。所以,这里是完整的代码:
# set factor levels
dat$Subject <- factor(dat$Subject, levels = dat$Subject[order(dat$Marks1)])
require(reshape2)
c.data <- melt(dat, id.var="Subject")
require(plyr)
n.data = ddply(c.data,.(Subject), transform, pos = cumsum(value) - 0.5*value)
require(ggplot2)
plot = ggplot(n.data, aes(x = Subject, y = value)) +
geom_bar(stat = "identity", colour = "#000000", aes(fill = variable)) +
geom_text(aes(label = value, y = pos), size = 3, face="bold", colour="white") +
scale_fill_manual(values=c("#FFFFFF", "#000000")) +
scale_y_continuous(limits=c(0, 100), breaks=seq(0,100, by = 10)) +
theme(axis.line = element_line(),
axis.text.x=element_text (angle=60,hjust=1,colour="black"),
axis.text.y=element_text(colour="black"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank(),
axis.ticks=element_blank()) +
labs(fill="") + scale_colour_discrete(breaks=NULL)
这是情节: