将Axis Tick标签添加到堆积条形图

时间:2014-02-08 15:29:38

标签: r ggplot2 geom-bar

我有六个人的表现数据集。我想显示“最小”和“目标”性能 - 在本例中,分别为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)

然而,我无法像顶部示例中那样拆分条形图。请问有人可以建议如何使标签和分割条工作吗?

由于

1 个答案:

答案 0 :(得分: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