I have a dataset for the performance of six people. I wanted to display a "minimum" and "target" performance - in this example, 100 and 140 respectively.

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

I've split the data into three rows per person (for 0 up to minimum, minimum to target, and then the remainder.

There are more than six y-axis tick labels, and as a result, the names don't match up (for example, Ronnie has the highest performance, not Mick).

I previously had one row per person (with working labels), like this:

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

However I wasn't able to split the bars as in the top example. Please could anyone suggest how to get the labels and split bars working?

Thanks

有帮助吗?

解决方案

Something like this?

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top