# 在R中对多列数据进行排序

``````Subject  Marks1  Marks2

ABC      45       55
CDE      34       66
DHD      54       46
XYZ      35       65
YXS      56       44
YDT      88       12
``````

``````n.data = ddply(c.data,.(subject), transform, pos = cumsum(value) - 0.5*value)

plot = ggplot(n.data, aes(x = subject, y = value)) +
geom_text(aes(label = value, y = pos), size = 3, face="bold", colour="white") +

labs(fill="")
``````

``````c.data <- melt(dat, id.var="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)
``````

0 条评论