0

我有一个六人表现的数据集。我想显示“最低”和“目标”性能——在这个例子中,分别是 100 和 140。

require("ggplot2")
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
rMat<-data.frame(c(rep(100,6),rep(40,6),20,21,27,46,138,168),
        c(rep(1:6,3)),c(rep(stones,3)))
colnames(rMat)<-c("X1","X2","X3")
colors <- c("white","#F2F2F2","#E5E5FF", "white","#F2F2F2","#CCCCFF", "white","#F2F2F2","#B2B2FF","white","#F2F2F2", "#9999FF", "white","#F2F2F2","#7F7FFF","white","#F2F2F2","#6666FF")
gp<-ggplot(NULL,aes(X2,X1),label=rROC[,1])+theme_bw()
gp<-gp+geom_bar(data=rMat,stat="identity",fill=colors,colour="gray")
gp<-gp+coord_flip()
gp<-gp+ylab("Performance")+xlab("")
gp<-gp+scale_x_discrete(labels=rMat[,3])
gp

我已将数据分成每人三行(从 0 到最小值,从最小值到目标,然后是其余的。

y 轴刻度标签超过六个,因此名称不匹配(例如,Ronnie 的性能最高,而不是 Mick)。

我以前每人有一排(带有工作标签),如下所示:

perf<-c(160,161,167,186,278,308)
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
rMat<-data.frame(perf,c(1:6),stones)

但是,我无法像上面的示例那样拆分条形图。请任何人建议如何让标签和拆分条工作?

谢谢

4

1 回答 1

0

像这样的东西?

pmin <- 40
pmax <- 100
stones<-c("Mick","Keith","Brian","Bill","Charlie","Ronnie")
p    <- c(20,21,27,46,138,168)
gg   <- data.frame(stones,p)
gg$stones <- factor(gg$stones, level=unique(gg$stones))
gg$status <- ifelse(gg$p<pmin,-1,ifelse(gg$p<=pmax,0,1))
ggp <- ggplot(gg)
ggp <- ggp + geom_bar(aes(x=stones, y=p, fill=factor(status)),stat="identity")
ggp <- ggp + geom_hline(yintercept=pmin, linetype=2, colour="red")
ggp <- ggp + geom_hline(yintercept=pmax, linetype=2, colour="blue")
ggp <- ggp + scale_fill_discrete("Status", labels=c("Below Min","Within Range","Above Max"))
ggp <- ggp + labs(x="",y="Performance")
ggp <- ggp + theme_bw()
ggp <- ggp + coord_flip()
ggp
于 2014-02-08T16:50:39.310 回答